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

93
Vistas
Get URL response code and store it in variable using https.get

I'm simply using https.get to retrieve a URL response code, but I don't want to just show it in the console, I want to store it in array to later insert it in a DB table. Here's what I'm doing:

results.forEach((element, index) => {
 link[index] = element.AppURLAndroid;
 https.get(link[index], function(res) {
   resCode[index] = res.statusCode;
  }).on('error', function(e) {
  console.error(e);
  });                
});

As you can see, I'm retrieving the URLs from an array, getting the response code, and trying to store it in another array. The problem relies in this line resCode[index] = res.statusCode; .. if I do console.log it works fine and I can see the response code, but somehow I just can't assign it into a variable or array. I also tried using a global variable like global.resCodeTemp, but it also doesn't work. What am I doing wrong?

Thank you.

[EDIT] : there are no error thrown, I just try to print the array after the insertion and the array is empty.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Can you please try below code. In below code I have pass index in function to use in rescode.

results.forEach((element, index) => {
 link[index] = element.AppURLAndroid;
 https.get(link[index], function(res,index) {
   resCode[index] = res.statusCode;
  }).on('error', function(e) {
  console.error(e);
  });                
});
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