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.
asynckeyword, you don't need to explicitly return a promise withreturn Promise.resolve(dealArray). You can simplyreturn dealArrayand it will be wrapped in a promise due to theasynckeyword.