Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return response of axios in return

Tags:

I want to return the response of axios but always the response that returned is undefined:

wallet.registerUser=function(data){ axios.post('http://localhost:8080/register',{ phone:data.phone, password:data.password, email:data.email }).then(response =>{   return response.data.message;   console.log(response.data.message); }).catch(err =>{   console.log(err); }) }  console.log(wallet.registerUser(data)); 

The console always logs as undefined. Is their any way returning this response.

like image 481
Candy Avatar asked Aug 10 '17 18:08

Candy


People also ask

How do I return a response from Axios call?

To return data from axios API with JavaScript, we can use the return statement. const axiosTest = async () => { const response = await axios. get(url); return response.

How do you get Axios response data in React?

First, you import React and Axios so that both can be used in the component. Then you hook into the componentDidMount lifecycle hook and perform a GET request. You use axios. get(url) with a URL from an API endpoint to get a promise which returns a response object.

Does Axios post return anything?

Making multiple concurrent GET requests. This section is a bonus section that covers how to perform multiple GET requests concurrently using Axios with error handling. Since Axios returns a promise, we can perform multiple GET requests using Promise.

What Axios get returns?

The axios. get makes an async request and returns a promise. let responses = await Promise.


2 Answers

console.log won't wait for the function to fully complete before logging it. This means that you will have to make wallet.registerUser asynchronous, there are two main ways to do this:

  1. Callback - this is when you pass a function as a parameter into your existing function which will be executed once your axios call has finished. Here is how it would work with your code:

    wallet.registerUser=function(data, callback){   axios.post('http://localhost:8080/register',{     phone:data.phone,     password:data.password,     email:data.email   }).then(response =>{     callback(response.data.message);     console.log(response.data.message);   }).catch(err =>{     console.log(err);   }) }  wallet.registerUser(data, function(response) {   console.log(response) }); 
  2. Promise - The easiest way to do this is to put async in front of your function name. This will make anything returned from the function return in the form of a promise. This is how it would work in your code:

     wallet.registerUser=async function(data){   axios.post('http://localhost:8080/register',{     phone:data.phone,     password:data.password,     email:data.email   }).then(response =>{     return response.data.message;     console.log(response.data.message);   }).catch(err =>{     console.log(err);   }) }  wallet.registerUser(data).then(function(response) {   console.log(response); }); 

Here is some more information on asynchronous functions:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

like image 181
Showard180 Avatar answered Oct 04 '22 19:10

Showard180


You can use Promises:

I am new to react so you can give suggestions to me to improve my code.

For example I want to call checkVerified() function which is in other file:

Utils.js

export function checkVerified(val, values) {     return new Promise((resolve, reject) => {         axios             .get(`${getInitialServerUrl}/core/check_verified/${val}/`)             .then(res => {                 resolve(res)             })             .catch(err => reject(err))     }) } 

And I am calling this function from an another file:

someFile.js

const handleSubmit = e => {         e.preventDefault();         checkVerified(values.mobile, props)             .then(res => {                console.log(res);          })         .catch(err => console.log(err.response))     } 
like image 25
Nikhil Bhardwaj Avatar answered Oct 04 '22 18:10

Nikhil Bhardwaj