我正在编写一个使用 Firebase 的 Web 应用程序,并从结构如下的数据库中读取信息:
"user" : {
"-KcNmMmqbSvmVXx2vn4A" : {
"email" : "firebase@firebase.com",
"firstname" : "fire",
"membership_tier" : 1,
"session_count" : 0,
"surname" : "base"
}
我正在尝试读取用户进行的 session 数 - 但未返回任何结果。
到目前为止我尝试了什么:
firebase.database().ref('/users/' + uid).once('value').then(function(snapshot) {
var sessionCount = snapshot.val().session_count;
});
当前 snapshot.val()
正在返回 null
。我收到一条错误消息:
Uncaught (in promise) TypeError: Cannot read property 'session_count' of null
uid
已定义,因此访问用户信息应该没有问题。
如有任何指点,我们将不胜感激!
最佳答案
以下对我有用:
//INITIALIZE FIREBASE WEB APP
firebase.initializeApp(config);
var db = firebase.database();
var auth = firebase.auth();
db.ref('/whatever/').once('value').then(function(snap) {
if(snap.val()){
console.log(snap.val().whateverProperty);
} else {
console.log('/whatever/whateverProperty node does not exist!');
}
}, function(error) {
// The Promise was rejected.
console.log(error);
});
关于javascript - Firebase 无法使用 "snapshot"访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43196547/