I’ve been searching for hours and still dont understand what’s the problem with my code. I’ve got a main function called “req” and in this function there’s another arrow function called “data”. When I try to access the “author” and “collection” parameters of the main function, I get a Reference Error : the variables are undefined. Thanks for your help !
const puppeteer = require('puppeteer');
const fs = require('fs');
function saveData (jsonData) {
fs.writeFile("myData.txt", jsonData, function(err) {
if (err) {
console.log(err);
}
});
}
async function req (author, collection) { // The two parameters are here : author and collection
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
let link = 'www.myexampleurl/'+ author + '/'+ book + '/';
await page.goto(link);
const data = await page.evaluate(() => { // Here's the arrow function
let elements = document.querySelectorAll('span.alldetails');
let myBooks = [];
for(element of elements) {
myBooks.push(
{
authorName : author, // !!!!! UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: author is not defined
collectionName : collection, // !!!!! UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: collection is not defined
description : element.description
}
)
}
return myBooks;
});
saveData(JSON.stringify(data)); // A simple function that works to save the data in a text file
browser.close();
};
req('john_doe', 'the_best_jd_books');