从firebase获取值

时间:2016-02-24 16:03:31

标签: javascript firebase

我希望在firebase中的函数之外获取一个全局使用的值,但它不起作用。

var ref = new Firebase("https://xxxxx.firebaseio.com/users/xxxx/roles/admin/"); // true or false

    ref.once("value", function(snapshot) {
    console.log("here");
    console.log(snapshot.val());
    var isAdmin = snapshot.val();
    return isAdmin;
});

console.log(isAdmin); /// cannot get value

1 个答案:

答案 0 :(得分:1)

您不能在外面使用isAdmin,因为它不可用,请在回调中使用

ref.once("value", function(snapshot) {
    //You should wrap you code here, to use isAdmin variable
    //You can do whatever with isAdmin, but you can't return it.
    //It's callback that running asynchronously
    console.log(isAdmin)
})