0

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

  }
programmingGal
  • 65
  • 1
  • 2
  • 11
  • You do `return userID` (synchronously) **long before your axios call (asynchronously) response has arrived**. Read about those two ways of code execution. – connexo Sep 06 '18 at 20:06
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – connexo Sep 06 '18 at 20:12
  • @connexo - I read through that question but didn't see an answer that helped me. They suggested using promises and retrieving the response with the `then` block, but I'm doing that already. Do you have a suggestion? – programmingGal Sep 06 '18 at 21:14
  • That first answer has **all** the information you could possibly need to first understand and then solve your problem. – connexo Sep 06 '18 at 21:24
  • @connexo - I tried @FelixKling 's answer (the 1st answer) that uses promises with the `then` but my `userID` is still not getting set. – programmingGal Sep 06 '18 at 21:49

1 Answers1

0

Try this...

retrieveUserID: function()
{
  this.axios.get('getUserID.php' , { 
    params: {
      username: this.currentUser
    } 
  })
  .then(response => {
    var userID=0;
    console.log("retrieveUserID:response.data = " + response.data);
    userID=response.data;
   } )
  .catch((error )=> {this.submitSessionFailure(error);});
  return userID ;  // staying zero
} 
D Durham
  • 1,243
  • 2
  • 20
  • 42
  • tried adding `NULL` but gave this error: **Uncaught ReferenceError: NULL is not defined** . Also, did you mean to define `userID` outside the method? (I tried both ways and none worked) – programmingGal Sep 06 '18 at 17:02
  • Edited the answer with another option to try. It seems like a scoping issue on that variable. Putting it global (outside the function) should have worked. So you have a variable with that same name defined elsewhere by chance? – D Durham Sep 06 '18 at 20:08
  • In case this is a problem, I renamed `userID` to `theUserID` because the method that calls `retrieveUserID()` assigns the result to a local variable also named `userID`. However, when I tried your edit it gave me the error **userID is not defined** - because you returned `userID` outside the scope of where you defined it. – programmingGal Sep 06 '18 at 21:11