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

215
Visualizações
Pushing to an array not working in callback

See the comments in code to see what's actually happening/not happening. Basically within the function, the array.push() method does not seem to work and I cannot wrap my head around why. Any help would be appreciated.

 var locationArray = new Array;

        request(process.env.RESOURCE_SHEET, (error, response, html) => {
            if(!error && response.statusCode == 200) {

                const $ = cheerio.load(html);
                
                $("h3").each((i, lle) => {
                    const location = $(lle).text();
                    if(location.includes("Kansas")) return;
                    if(location.includes("In Stock")) {
                        var level = location + " ✅";
                    } else {
                        var level = location + " ❌";
                    }
                    locationArray.push(level); // This doesn't push to the array
                });
            } 
        });
        locationArray.push("test") // This pushes to the array

        console.log(locationArray) // Output: "test"
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Thanks to Harsh Saini.

His response: request works asynchronously, you should try console the array inside the request callback.

Working code:

var locationArray = new Array;

request(process.env.RESOURCE_SHEET, (error, response, html) => {
    if(!error && response.statusCode == 200) {

        const $ = cheerio.load(html);
            
        $("h3").each((i, lle) => {
            const location = $(lle).text();

            if(location.includes("Kansas")) return;
            if(location.includes("In Stock")) {
                var level = location + " ✅";
            } else {
                var level = location + " ❌";
            }
            locationArray.push(level);
        });
    } 
    console.log(locationArray)
});
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