简体   繁体   中英

async method always returning undefined

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM