I am using puppetter in node.js to get the data from a site, i am getting the data, but i am having a little problem with the promisse, i guess.
i gonna get this data and split it in two "variables" (country and league), but first a need to get the data from the site.
whats happening is that when i console.log inside the "foreach" its show me the data that i need, but out of it, it console.log a undefined value.I searched and saw that its because it is a promisse, but i am not being able to do it proppely.
i commented in the code what i am tryng to do....
try {
//using library puppeteer to get data from flashscore.com.br or the site you want
//you need to put the headless option as false to see the browser opening
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto("http://m.flashscore.com.br");
//getting the element that contains the games
const element = await page.$("div.soccer > div#score-data");
const dataCountryAndLeague = await element?.$$(`h4`,);
var data: string | null
dataCountryAndLeague?.forEach(element => {
element.getProperty('textContent').then(textContent => textContent.jsonValue().then(async (cld) => {
console.log(cld); //getting the country and league (THE Data its showed as ia want)
data = cld; //trying to pass the data to a variable
})
);
console.log(data); //trying to print the data outside the forEach, but its undefined
});
} catch (error) {
console.log(error + "Erro identificado ao tentar acessar os dados da página");
return error + "Erro identificado ao tentar acessar os dados da página";
}
Im am expecting to do what i explained before.
since alredy, thankyou for your atention.