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

114
Vistas
axios data is undefined

I'm trying to perform an axios get request.

  axios
  .get("http://10.10.0.145/api/session", {
      headers: {
          'Cookie' : user_cookie
      }
  },         
  )
  .then(res => {
  
      result = res.data;
      id_user_intervention = res.data.id;
      console.log(id_user_intervention); //IS NOT UNDEFINED

      
    })
  .catch(error => {
    console.error(error)
  })
  console.log(id_user_intervention); //IS UNDEFINED

I need to use id_user_intervention outside the axios request. I assign res.data.id to id_user_intervention, but this variable is undefined outside the axios request...how can I solve this problem?

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

0

First of all, it's better you learn async/await on javascript. Then, try the following solution:-

const testFunction = async () => {
  let id_user_intervention = null;
  try {
    const response = await axios.get("http://10.10.0.145/api/session", {
      headers: {
        'Cookie': user_cookie
      }
    })
    id_user_intervention = response.data.id;
  } catch (err) {
    console.error(err);
  }

  return id_user_intervention;
}

const anotherFunction = async () => {
  const data = await testFunction();
  console.log(data);
}

Hope it will work properly.

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