I am using puppeteer and node.js to scrape some data, but I am having some problems when trying to loop a list of url:s. When I push the scraped data, I got an error saying that the array is not defined. I think the problem has to do with using await inside the for loop, but I don't really understand why and how to fix it. Why does it says that the array is not defined?
Here is a very simplified version of my code:
const scrapeJobInfo = async (links) => {
/* Initiate the Puppeteer browser */
const browser = await puppeteer.launch();
const page = await browser.newPage();
/* Empty array for pushing the data */
const jobData = [];
/* For loop that push the data */
for(let i = 0; i < links.links.length; i++ ) {
let linkUrl = `${links.links[i]}`
await page.goto(linkUrl, { waitUntil: 'networkidle0' });
let companyInfo = await page.evaluate(() => {
jobData.push('hi') //<-- ReferenceError: jobData is not defined
});
}
/* Close browser and log jobData */
await browser.close();
console.log(jobData)
};