Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!