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 !