Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

437
Visualizações
UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: k is not defined

I'm trying to use for loop. But this error occurs. "ReferenceError: k is not defined" How can i fix the error?

    let imageURL = [];

    for (let k = 1; k <= images.length; k++) {
      let data = await page.evaluate(() => {
        let innerHTML = document.querySelectorAll('#__layout > main > div.nuxt-container > div > div:nth-child(3) > div.item-container > div:nth-child(72) > div > div.body > div.image-container > ul > li')[k].innerHTML;
        let startIndex = innerHTML.indexOf('&quot;') + 6;
        let endIndex = innerHTML.lastIndexOf('&quot;');
        let imageLink = innerHTML.substring(startIndex, endIndex);

        return {
          image: imageLink,
        };
      });
      imageURL.push(data);
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Suppose that you are using puppeteer...

var a = 3;
await page.evaluate(() => {
  // Here you cannot use 'a', since 'a' is in the context that is different than the context where this function is going to be executed (page context)
})

If you take a look at the api:

page.evaluate(pageFunction[, ...args])

You can see that the first argument is your function that will be executed in the page context. But you can see also, that you can have arguments (args) that will be passed to pageFunction.

Instead you should do:

var a = 4;
await page.evaluate((a) => {
  // Now you can use outer 'a' variable through argument passed 
}, a);

So if you have few arguments, accordingly:

await page.evaluate((k, startIndex, endIndex) => {
 
}, k, startIndex, endIndex);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda