2016-02-24 121 views
0

我想要获取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 
+0

另见:http://stackoverflow.com/questions/14220321/how-do-i-返回异步调用响应,其中详细解释了为什么无法工作。 –

回答

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) 
}) 
+0

那么,在全局或者外部使用“isAdmin”是不可能的?我需要在我的代码中包含这个函数的其余部分吗? – Jason

+0

@Jason是的,你是完全正确的 – isvforall