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

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

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