1

I have a function that calls an API and returns some data:

async function getDataAxios(){
        await axios.get("http://localhost:8080/cards/1").then(response => {
                console.log("RESP: ", response.data[0])
                return response
            })
            .catch(err => {
            console.log("Error: ", err)
            return err
        })
}

When I log response.data[0] inside getDataAxios() the expected data is present. But when I try and return the data to my calling function and log the return data it logs undefined:

getDataAxios().then(r => console.log(r))

I have also tried the following:

 const resp = getDataAxios().then(resp => console.log("Data 2: ", resp)).catch(
    err => {
        console.log("An error has occurred: ", err)
        return err;
    }
  )
    console.log(resp)

Full code:

function App() {
    getDataAxios().then(r => console.log(r))
}

async function getDataAxios(){
        await axios.get("http://localhost:8080/cards/1").then(response => {
                console.log("RESP: ", response.data[0])
                return response
            })
            .catch(err => {
            console.log("Error: ", err)
            return err
        })
}

export default App;
3
  • 4
    Your getDataAxios does not return anything. No need for it to be async, just do return axios.get(...).. Commented Dec 17, 2022 at 22:54
  • That does work, but i'm not really sure why. How is doing return axios.get("http://localhost:8080/cards/1") different from axios.get("http://localhost:8080/cards/1").then(resp => { console.log("RESP: ", resp.data[0]) return resp }) Commented Dec 17, 2022 at 23:02
  • The latter returns the value from the then callback, not from getDataAxios. The former will return the entire promise from getDataAxios, allowing it to be used outside. Commented Dec 18, 2022 at 0:29

2 Answers 2

1

Just adding here some more details, since the comment was not very detailed.

You have

async function getDataAxios(){
  await axios.get(...).then(...).catch(...);
}

and

function App() {
  getDataAxios().then(r => console.log(r))
}

The getDataAxios method does not return anything. The then inside it does return a value, but that is not returned from the actual getDataAxios function.

Nothing wrong with that on its own, if the then code is all you wanted to perform.

But you then call it in the App and use then on it, getDataAxios().then(r => console.log(r)). The r here will contain whatever was returned from the getDataAxios which is nothing, so the r will be undefined.

Adding the return to the getDataAxios will return the promise. And since the return value type of axios is a promise you do not have to specify async, nor await for it.

So you can use

function getDataAxios(){
  return axios.get(...).then(...).catch(...);
}

and now you can use it as you already do, as

function App() {
  getDataAxios().then(r => console.log(r))
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, but I am then unable to use the data returned from the getDataAxios() outside of the then(). If I try and get the assign the return value to a variable it returns a promise: const z = getDataAxios().then(r => console.log(r)) console.log(z)
@HLT for that you will have to return the r and the outside code will have to await the getDataAxios. So somthing like const z = await getDataAxios().then(r => { console.log(r); return r; }); console.log(z);
0

Turn the response into a constant and than store data in it and than return it. Do this instead.

async function getDataAxios(){
    await axios.get("http://localhost:8080/cards/1").then(response => {
            console.log("RESP: ", response.data[0])
            const response = response.data[0]
            return response
        })
        .catch(err => {
        console.log("Error: ", err)
        return err
    })
  }

Or you can do this

       async function getDataAxios(){
     await axios.get("http://localhost:8080/cards/1").then(response => {
            console.log("RESP: ", response.data[0])
            return response.data[0]
        })
        .catch(err => {
        console.log("Error: ", err)
        return err
    })
   }

1 Comment

Both of these functions return undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.