0

I can't solve that problem so I'm asking that here:

This is the async function, and as you can see is returning an array. But it returns an undefined value.

async function scrape(pageURL) {
var dealArray = [];
try {
    const browser = await puppeteer.launch({ headless: true });
    const page = await browser.newPage();
    await page.goto(pageURL);
    await page.waitForSelector('div.s-item-container');
    const dealsElements = await page.$$('div.s-item-container');
    for(deal of dealsElements) {
        let dealTitleElement = await deal.$('div.s-item-container a.s-access-detail-page');
        let dealTitleValue = await (await dealTitleElement.getProperty('title')).jsonValue();
        let dealPriceElement= await deal.$('div.s-item-container span.a-color-price');
        let dealPriceValue = await (await dealPriceElement.getProperty('textContent')).jsonValue();
        let dealReviewsElement = await deal.$('div.s-item-container .a-icon-star');
        let dealLinkValue = await (await dealTitleElement.getProperty('href')).jsonValue() + '&tag=dragonstv-21';
        let dealReviewsClass = await (await dealReviewsElement.getProperty('className')).jsonValue();
        let dealReviewsValue;
        if(dealReviewsClass) {
            let starValue = dealReviewsClass.substring(26);
                if(starValue.indexOf('-') === -1) {
                    dealReviewsValue = starValue;
                } else {
                    let stars = starValue.replace('-', '.');
                    dealReviewsValue = stars;
                }
        }
        dealArray.push({
            "title": dealTitleValue,
            "price": dealPriceValue,
            "reviews": dealReviewsValue + "/5.0",
            "link": dealLinkValue,
            "store": "Amazon",
        });
    }
    return Promise.resolve(dealArray);
} catch(e) {
    console.error('Error: ' + e);
}
}

And here is how I'm calling it:

scrape('working link').then((data) => {
    console.log(data) // result: undefined
}

It works only if I declare the variable out of the function and the function doesn't return anything but only changes the array content.

1
  • Note that because your function is declared with the async keyword, you don't need to explicitly return a promise with return Promise.resolve(dealArray). You can simply return dealArray and it will be wrapped in a promise due to the async keyword. Commented Jan 11, 2019 at 22:11

2 Answers 2

1

As written, your function must return an array (empty or otherwise). If it's returning undefined, then you're generating an exception and should see one in the console, via your catch statement. If you're not seeing it, you might try removing the try/catch and see what exception bubbles up.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like the last iteration gives a null value. I'm going to solve it myself.
0

I've actually figured the problem out. It was returning a string so I had to use JSON.parse(request) so I have an object on which I can work on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.