gpt4 book ai didi

javascript - Firebase - snapshot.val() 返回 null - 为什么它无法连接到正确的集合?

转载 作者:行者123 更新时间:2023-11-30 20:28:30 25 4
gpt4 key购买 nike

我正在尝试从我的 firebase 集合中的字段中检索信息,以便我可以在我的网站上使用该信息。但是,snapshot.val() 给了我 null。我的引用是否没有正确定位集合?当我尝试访问集合中的字段时(正如您在注释掉的行中看到的那样),我收到此错误:

headerNav.vue?c47a:69 Uncaught (in promise) TypeError: Cannot read property 'userControl' of null

this is an image of how my database is set up

这个问题单独让我获得了一个 StackOverflow 帐户。你们能理解吗?如果你能教我如何解决这个问题,我很乐意被证明是个白痴。

  if(firebase.auth().currentUser){

return firebase.database().ref('/users/samgmailcom').once('value').then((snapshot) => {
console.log(snapshot.val());
//console.log(snapshot.val().userControl);
});

} else {
this.user = null
}

最佳答案

您在屏幕截图中显示的数据库是 Cloud Firestore。但您访问的 API 是针对 Firebase 实时数据库的。它们是完全独立的数据库,每个数据库都有自己的 API。

按照 getting data from Cloud Firestore 上的文档进行操作解决问题。像这样的东西应该可以解决问题:

var docRef = db.collection("users").doc("samgmailcom");

docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});

关于javascript - Firebase - snapshot.val() 返回 null - 为什么它无法连接到正确的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614780/

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