我希望在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 :(得分: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)
})