1

I created this function:

var IDOfUserToKill = 'tralala';

export function getIDOfUserToKill(userID, gameID) {
  let path = '/games/' + gameID + '/users/' + userID;

  fb.database().ref(path).on('value', snapshot => {
      IDOfUserToKill = '';

      if (snapshot.val()) {
        IDOfUserToKill = snapshot.val().userToKill;
        console.log(IDOfUserToKill); // return the good ID
      }
    });
}

userID = IDOfUserToKill; // return "tralala" and not the good ID

Then I want to use the variable IDOfUserToKill outside this one. Because if I use IDOfUserToKill, it returns the value that was defined before the function (tralala) and not the ID.

How to retrieve the content of the variable? (I use React Native).

31piy
  • 23,323
  • 6
  • 47
  • 67
Jean
  • 201
  • 4
  • 12
  • Is ```fb.database().ref(path).on('value', snapshot => {}``` asynchronous ? Because that would explain why you don't log the correct value. You are probably assigning `IDOfUserToKill` to `userID` before the function finished executing. – DrunkDevKek Mar 09 '18 at 11:28
  • Why do you expect that an export of a function (note that you do not call it) will change the value of `IDOfUserToKill`? – trixn Mar 09 '18 at 11:41
  • No, I just exported the function because I'm using it in a different file. – Jean Mar 09 '18 at 15:20
  • So how do I recover the content? Do you have any examples? – Jean Mar 09 '18 at 15:56

1 Answers1

1

This function:

 fb.database().ref(path).on('value', snapshot => {
  IDOfUserToKill = '';

  if (snapshot.val()) {
    IDOfUserToKill = snapshot.val().userToKill;
    console.log(IDOfUserToKill); // return the good ID
  }
});

is asynchronous which means it moves on to another task before it finishes.

That is why inside on() you get the id that is in the database, while outside, here userID = IDOfUserToKill; you get the value 'tralala'.

To learn more about asychronous check this:

https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • I this case it doesn't even matter if it is asynchronous or not. OP defines a variable `IDOfUserToKill` then exports a function (without calling it) and the assigns the value of `IDOfUserToKill` to another variable which of course has the current value of `IDOfUserToKill` which is `'tralala'`. – trixn Mar 09 '18 at 11:39
  • @Jean did the answer help you? – Peter Haddad Mar 09 '18 at 13:33
  • Okay, I understand the principle. So how do I recover the content? Do you have any examples? – Jean Mar 09 '18 at 15:21
  • @Jean here check this, same problem that you have: https://stackoverflow.com/questions/11636731/handling-asynchronous-calls-firebase-in-functions Tell me if you did not understand it. Also upvote and mark as correct this answer so people know it is helpful – Peter Haddad Mar 09 '18 at 17:16
  • I don't really understand, can you give me an example with my function? – Jean Mar 09 '18 at 17:52
  • @Jean you can just add this `userID = IDOfUserToKill` instead `on()` and when u want to use `userID` just use it inside the function so you get the right value – Peter Haddad Mar 09 '18 at 18:37
  • @Jean check this: https://developers.google.com/web/fundamentals/primers/promises – Peter Haddad Mar 09 '18 at 18:57
  • Okay, but isn't there a way to get the content out of the function? – Jean Mar 09 '18 at 20:11