Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem calling Axios, returning data from .then arrow function is always undefined

I have the following code:

getData = async function (afgJSON) {
      console.log("step 2")
      await axios.post(process.env.afgEndPoint, afgJSON, {
          headers: headers
        })
        .then((response) => {
          console.log("step 3")
          return response.data
        })
        .catch((e) => {
          console.log("step 3")
          return e.response.data    
        })
    }

Which I call as this:

console.log("step 1")
let afgResponse = await common.getData(afgJSON);
console.log("step 4")
console.log(afgResponse)

afgResponse always is undefined, and the console.logs show the right order:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

step 1

step 2

step 3

step 4

undefined

… but if I console.log response.data (.then), the response is fine.

I was reading other posts at stackoverflow about .then => with Axios but I still cant figure this out.

Thanks.

>Solution :

You missed to return the result of axios call from the function "getData"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading