Skip to content
Advertisement

Returning data from Axios API [duplicate]

I am trying to use a Node.JS application to make and receive API requests. It does a get request to another server using Axios with data it receives from an API call it receives. The second snippet is when the script returns the data from the call in. It will actually take it and write to the console, but it won’t send it back in the second API.

JavaScript

JavaScript

I’m aware this is wrong, I’m just trying to find a way to make it work. The only way I can seem to get data out of it is through console.log, which isn’t helpful in my situation.

Advertisement

Answer

The issue is that the original axiosTest() function isn’t returning the promise. Here’s an extended explanation for clarity:

JavaScript

The function can be written more succinctly:

JavaScript

Or with async/await:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement