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

97
Visualizações
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 Respostas
Responde à pergunta

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 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