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

159
Vistas
Value of Javascript JSON obj "undefined"

Hey I´m getting this response query a API with node fetch:

{
  'access-control-allow-credentials': 'true',
  'access-control-allow-headers': 'Origin, Content-Type, Accept, Authorization, X-Request-With, Webpunch-App-Version, X-timezone, X-token',
  'access-control-allow-methods': 'POST, GET, OPTIONS, PUT, DELETE',
  'access-control-allow-origin': 'censored',
  'access-control-max-age': '86400',
  'cache-control': [ 'no-store, no-cache, must-revalidate', 'no-cache, private' ],
  'content-encoding': 'gzip',
  'content-type': 'application/json',
  date: 'Sun, 29 May 2022 13:04:49 GMT',
  expires: 'Thu, 19 Nov 1981 08:52:00 GMT',
  pragma: 'no-cache',
  'set-cookie': [
    'QWFMSESSION=censored; path=/; secure; HttpOnly',
    'api_session=meJMkISigaS2dcHK7kKBzFjWyY4zHiQHnyKOiix4; expires=Sun, 05-Jun-2022 13:04:49 GMT; Max-Age=604800; path=/; secure; httponly; samesite=lax'
  ],
  'strict-transport-security': 'max-age=15724800; includeSubdomains; preload',
  'transfer-encoding': 'chunked',
  vary: 'Accept-Encoding',
  'x-content-type-options': 'nosniff',
  'x-frame-options': 'SAMEORIGIN',
  'x-xss-protection': '1; mode=block'
}

And want to get the 'api_session' token value. When I try to use result['set-cookie'] it returns undefined. When i try use JSON.parse(JSON.stringify(result)) it returns a blank array. But when i copy the result and paste it to another js file it works without any problems... here´s my code:

fetch('censored', {
    method: 'POST',
    headers: {
        censored
    },
    body: censored
    
})
.then(async (res) => {  
    return [await res.json(), res.headers];
  })
  .then(([jsonData, result]) => {
    console.log(result['set-cookie'])
  })
  .catch((err) => {
    // handle error
    console.error(err);
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

fetch('')
.then(async (res) => {  
  return [await res.json(), res.headers];
})
.then(([jsonData, result]) => {
  console.log(jsonData['set-cookie'])
})
.catch((err) => {
  // handle error
  console.error(err);
});

You need to read from jsonData and not from result

Result сontain request headers

If you wanna get api-session from header try this

fetch('')
.then(async (res) => {  
  return [await res.json(), res.headers];
})
.then(([jsonData, result]) => {
  console.log(result.get('api_session'))
})
.catch((err) => {
  // handle error
  console.error(err);
});
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