gpt4 book ai didi

javascript - 从 firebase 获取值

转载 作者:行者123 更新时间:2023-12-03 07:36:06 25 4
gpt4 key购买 nike

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

最佳答案

您不能在外部使用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)
})

关于javascript - 从 firebase 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35607030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com