drawrowfly / amazon-product-api

Amazon Scraper. Scrape products from the amazon search result or reviews from the specific product
627 stars 181 forks source link

Need to try and catch an error with calculation for product_information.available_from #39

Open guy032 opened 3 years ago

guy032 commented 3 years ago

I checked this link: https://www.amazon.es/dp/B08739TY2T And without my suggestion here for Amazon.js:

        // Calculate months and days on amazon
        try {
            if (product_information.available_from) {
                const from = moment(new Date(product_information.available_from));
                const now = moment(new Date());
                const duration = moment.duration(now.diff(from));
                product_information.available_from_utc = new Date(product_information.available_from).toISOString();
                product_information.available_for_months = Math.ceil(duration.asMonths());
                product_information.available_for_days = Math.ceil(duration.asDays());
            }
        } catch {}

It returns null for asin({ asin: "B08739TY2T", country: "ES" })

The reason is:

RangeError: Invalid time value
    at Date.toISOString (<anonymous>)
    at AmazonScraper.extractProductInfromation (amazon-buddy\lib\Amazon.js:694:103)
    at AmazonScraper.grapAsinDetails (amazon-buddy\lib\Amazon.js:1060:74)
    at amazon-buddy\lib\Amazon.js:238:30

The try and catch fix this issue.