variable no accessible after a Firebase call [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-12 02:55:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!