试图从firebase获取变量的快照给我一个错误 [英] Trying to get a snapshot of a variable from firebase gives me an error
问题描述
在我使用本机和firebase做的社交媒体应用中,我试图获取评论数量post使用我保存在服务器上的变量的快照功能,然后当用户添加新注释时,我将向此变量添加一个。我这样做的代码就在这里:
In a social media app I am making with react native and firebase, I am trying to grab the number of comments a post has using the snapshot function of a variable I have saved on my servers, then I am going to add one to this variable when a user adds a new comment. My code to do so is right here:
firebase.database().ref('posts').child(this.state.passKey).update({
comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1
})
当我实际运行此代码时,我收到错误消息:
When I actually run this code, I get an error saying:
Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
起初我认为这可能是this.state.passKey实际上没有传递密钥,而是放入我从服务器复制的密钥解决问题。
At first I thought this might be that the "this.state.passKey" wasn't actually passing the key, but putting in a key I copied from the server didn't fix the problem.
我的服务器
-
My Server -
推荐答案
要获得特定帖子的评论,你应该这样做
To get the comments of particular post you should do like this
let postId='someId'
postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
});
这篇关于试图从firebase获取变量的快照给我一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!