简体   繁体   English

异步方法总是返回未定义

[英]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. 如果返回undefined ,那么您正在生成一个异常,应该通过catch语句在控制台中看到一个异常。 If you're not seeing it, you might try removing the try/catch and see what exception bubbles up. 如果您没有看到它,则可以尝试删除try / catch并查看出现了什么异常。

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. 它正在返回一个字符串,所以我不得不使用JSON.parse(request),所以我有一个可以处理的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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