2016-02-24 62 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