问题
I'm using Firebase to access to same data, but i can't use the data out of the function used to access it. How i can make a console log of the data just accessed instead of the initial value?
var whatNeed = 'hello';
gameData = new Firebase('https://thegame.firebaseio.com/gameData');
gameData.on("value", function(snapshot) {
var data = snapshot.val();
whatNeed = data.property;
});
console.log(whatNeed);
回答1:
whatNeed will always contain the initial value when called outside of your callback as the console.log will be called first since the value event handler is asynchronous.
来源:https://stackoverflow.com/questions/27474546/variable-no-accessible-after-a-firebase-call