I have been researching this question for some time and have not found an answer.
I am using axios.get
to return the userID
. I am getting the correct response - I get a number for userID
when doing console.log(response.data);
However, when I try to assign response.data
to a local variable so I can return it, it is not getting set.
( I also tried defining userID
globally under data
and then referencing it as userID.this
but it didn't help.)
I've looked at other answers such as this one: Axios can't set data but they didn't work for me.
Any help is appreciated. Thanks in advance. (code below)
edit: I also looked at this question: How do I return the response from an asynchronous call? but it didn't add anything for me. It suggested using promises and the then
block but I did that already and it's still not working.
retrieveUserID: function()
{
var userID=0;
this.axios.get('getUserID.php' , {
params: {
username: this.currentUser
} })
.then(response => {
console.log("retrieveUserID:response.data = " + response.data);
userID=response.data;
} )
.catch((error )=> {this.submitSessionFailure(error);});
return userID ; // staying zero
}