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

93
Visualizações
Node-fetch Promise response property prints as undefined

I know similar questions have been asked a bunch (see here and here), but I've tried the solutions mentioned there I'm trying to figure out why my node-fetch response isn't behaving as expected. Here's a simple version of the code:

 fetch(new URL("https://www.stackoverflow.com")).then(theResponse =>{
      console.log("This prints OK: "); 
      console.log(theResponse.headers);
      console.log("But this prints as undefined:");
      console.log(theResponse.headers["content-encoding"]);
      return theResponse;
})

The headers as printed are:

{
'accept-ranges': 'bytes',
'cache-control': 'private',
 connection: 'close',
'content-encoding': 'gzip',
 'content-security-policy': "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com",
etc.
}

Why is theResponse.headers["content-encoding"] returning as undefined?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to use the .get method of the Headers object:

console.log(theResponse.headers.get("Content-Encoding"));

Notice that unlike a Map or object property access, it works case-insensitively.

about 4 years ago · Juan Pablo Isaza Relatório

0

you can also use the method getAll of Headers object it return an array

console.log(theResponse.headers.getAll("Content-Encoding"));

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