Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

436
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda