Firebase调用后无法访问的变量 [英] variable no accessible after a Firebase call

查看:59
本文介绍了Firebase调用后无法访问的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase来访问相同的数据,但是我无法在用于访问它的功能中使用这些数据。
如何使刚访问的数据而不是初始值成为控制台日志?

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);


推荐答案

需要在回调之外被调用时将始终包含初始值,因为 value console.log $ c>事件处理程序是异步的。

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.

这篇关于Firebase调用后无法访问的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆