我正在创建一个 HTML 页面,用户可以在其中查看并编辑所有帖子。目前,我只是在控制台记录结果,看看是否获得了所需的数据,但没有收到任何错误或数据。
页面应仅显示当前登录用户的帖子,但我下面的代码不会返回任何内容,即使 Firebase 数据库中有该用户的帖子。
关于如何仅显示当前登录用户的帖子有什么想法吗?
以下是我的数据的设置方式:
{
"posts" : {
"-KnP3MkFt5559uP1w5fh" : {
"content" : "content 1",
"title" : "title 1",
"uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
},
"-KnPPwHhfXDv34Lr0HMP" : {
"content" : "content 2",
"title" : "title 2",
"uid" : "B3nHbd7G49Y2iwSA3tJSaHFVuCq2"
},
"-KnPR_ZsgOwrpRDGZkHT" : {
"content" : "content 3",
"title" : "title 3",
"uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
}
},
"profiles" : {
"B3nHbd7G49Y2iwSA3tJSaHFVuCq2" : {
"email" : "-",
"firstName" : "-",
"lastName" : "-"
},
"O96P2INZn6S42dqqwgtz2OfgmYb2" : {
"email" : "-",
"firstName" : "firstFirst",
"lastName" : "firstLast"
}
},
"user-posts" : {
"-KnP3MkFt5559uP1w5fh" : {
"content" : "content 1",
"title" : "title 1",
"uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
},
"-KnPPwHhfXDv34Lr0HMP" : {
"content" : "content 2",
"title" : "title 2",
"uid" : "B3nHbd7G49Y2iwSA3tJSaHFVuCq2"
},
"-KnPR_ZsgOwrpRDGZkHT" : {
"content" : "content 3",
"title" : "title 3",
"uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
}
}
}
这是我的 JavaScript:
firebase.auth().onAuthStateChanged(function (firebaseUser) {
if (firebaseUser) {
var userid = firebase.auth().currentUser.uid;
var myPostsRef = firebase.database().ref('user-posts');
myPostsRef.orderByChild('uid').equalTo('userid').on('child_added', function(snapshot) {
snapshot.forEach(function(snapshot) {
var myPostData = snapshot.val();
console.log(myPostData);
});
});
最佳答案
这个:
myPostsRef.orderByChild('uid').equalTo('userid')
..看起来应该是这样的:
myPostsRef.orderByChild('uid').equalTo(userid)
经常发生:)
关于javascript - 查询 Firebase 数据库但未获取数据或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44737534/