Reputation: 311
I am trying to use the data I retrieve from an axios call. I get the correct info back in the response but when I try to return response I get undefined in the calling function. Is there a different way to return the response.data to the calling function?
static getRequest(url) {
require('es6-promise').polyfill();
axios({
method: 'get',
url: url,
responseType:'json',
withCredentials: true
})
.then(response => {
console.log(response.data);
return response.data;
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log('___________ERROR RESPONSE__________');
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log('_________ERROR REQUEST_______');
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an
Error
console.log('Error', error.message);
}
console.log('_________ERROR CONFIG_________');
console.log(error.config);
});
}
Upvotes: 3
Views: 4232
Reputation: 12129
You also need to return your axios
call from your getRequest
function.
In the code you have above you are only returning your axios promise. The following code will return the value of the response
when getRequest
is invoked.
static getRequest(url) {
return axios({
method: 'get',
url: url,
responseType:'json',
withCredentials: true
}).then(response => {
return response.data
})
//rest of code here
}
Upvotes: 3