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.
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.
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.
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.
The axios. get makes an async request and returns a promise. let responses = await Promise.
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:
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) });
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
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:
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:
const handleSubmit = e => { e.preventDefault(); checkVerified(values.mobile, props) .then(res => { console.log(res); }) .catch(err => console.log(err.response)) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With