0

I have a function which is returning data from my Firebase database like this :

function data(value) {
    ref.child('users').child(auth().twitter.id).child(value).once("value", function(snapshot) {
        data = snapshot.val();
     });
    return data;
}

And then, I have a code when user is logging in like this :

$("#twitter").click(function() {
        ref.authWithOAuthPopup("twitter", function(error, authData) {
          if (error) {
            console.log("Login Failed!", error);
          } else {
            var lol = ref.child('online').push({'displayName' : auth().twitter.displayName.toString()});
            lol2 = lol.name();
            console.log(lol2);
            ref.child('users    ').child(auth().twitter.id).once('value', function(snap) {
                if(!snap.exists()) {
                   ref.child('users').child(auth().twitter.id).set({
                        'pseudo' : auth().twitter.displayName, 
                        'money' : 0,
                        'score' : 0
                    });
                }
            });
            $("#user").html('<li class="pure-menu-item"><a class="pure-menu-link"><b><img src="'+auth().twitter.profileImageURL+'" width="16" height="16" style="margin-right:5px;" />'+auth().twitter.displayName+'</b></a></li></li>');
            $("#money").html('<li class="pure-menu-item"><a class="pure-menu-link"><b><img src="img/icons/money.png" width="16" height="16" style="margin-right:5px;" />'+data('money')+'</b></a></li></li>');
          }
        });
    });

But with this code, the li created returns my function code data(), and if I put 'online' on this line :

ref.child('online').child(auth().twitter.id).once('value', function(snap) {

It returns the correct answer, namely 0 (or any other number, but always 0 because id does not exist because 'online' is not the correct child, the correct one is 'users', so...)

Can someone tell my why ? :v Thanks !

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • up, I tested some things but nothing works :/ – Vincent ROUILLARD MELTZ May 29 '16 at 19:36
  • Data from Firebase is loaded asynchronously. While the data is being loaded, you code continues to execute (so that the browser stays responsive). Once the data comes back, your callback funciton is called. See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Frank van Puffelen May 29 '16 at 20:37
  • Actually, my function as you can see is something like function thing() { blabla }, and when a use this function inside of my code, I got "function thing() { blabla }" thus I did an other function and now it works, but I don't like the way I use. :/ – Vincent ROUILLARD MELTZ May 29 '16 at 20:51

0 Answers0