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

174
Vistas
Javascript : Parameters in a parent function are undefined in child function

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');
about 4 years ago · Santiago Gelvez
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