1

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.

  • i kinda that understood that i am tryng to get the value before its promisse return anything. Ok, but i tryed to do it correctly, putting a await or async, and its still dint workin. i know that i am doing something wrong but i just dont konw what. – Bruno Araújo Feb 05 '23 at 13:56

1 Answers1

1

We're gonna have to wait for all the promises to complete. For example:

async function handle(element) {
  const textContent = await element.getProperty('textContent');
  return textContent.jsonValue();
}

const promises = dataCountryAndLeague?.map(element => handle(element)) ?? [];

const data = await Promise.all(promises);
console.log(data) // All your data goes here
hungtran273
  • 1,180
  • 9
  • 11
  • 2
    Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Feb 05 '23 at 15:00
  • 1
    I don't know this is an antipattern. Answer edited. Thanks so much @Bergi – hungtran273 Feb 05 '23 at 15:11