Javascript 尝试从firebase获取变量的快照时出错 问题

Javascript 尝试从firebase获取变量的快照时出错 问题,javascript,firebase,react-native,firebase-realtime-database,Javascript,Firebase,React Native,Firebase Realtime Database,在我使用react native和firebase制作的社交媒体应用程序中,我尝试使用保存在服务器上的变量的快照功能获取帖子的评论数,然后当用户添加新评论时,我将向该变量添加一条评论。我的代码如下: firebase.database().ref('posts').child(this.state.passKey).update({ comments: firebase.database().ref('posts/'+this.state.passKey).child('comments'

在我使用react native和firebase制作的社交媒体应用程序中,我尝试使用保存在服务器上的变量的快照功能获取帖子的评论数,然后当用户添加新评论时,我将向该变量添加一条评论。我的代码如下:

firebase.database().ref('posts').child(this.state.passKey).update({
   comments: firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1 
})
当我实际运行这段代码时,我得到一个错误,它说:

Reference.child failed: First argument was an invalid path = "undefined".
Paths must be non-empty strings and can't contain ".","#","$","[", or "["
起初我认为这可能是因为“this.state.passKey”实际上没有传递密钥,但输入从服务器复制的密钥并不能解决问题

我的服务器
-

看起来您需要以下代码来查询数据库:

firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1 
不幸的是,它不是那样工作的。由
child()
ref()
返回的数据库对象上没有
snapshot
属性

相反,您需要在该引用处查询数据库,然后当您被回调其值时,您可以将其应用到其他地方

var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
    // use the snapshot here
})

看起来您需要以下代码来查询数据库:

firebase.database().ref('posts/'+this.state.passKey).child('comments').snapshot.val() + 1 
不幸的是,它不是那样工作的。由
child()
ref()
返回的数据库对象上没有
snapshot
属性

相反,您需要在该引用处查询数据库,然后当您被回调其值时,您可以将其应用到其他地方

var ref = firebase.database().ref('posts/'+this.state.passKey+'/comments')
ref.once('value', function(snapshot) {
    // use the snapshot here
})

为了得到特定帖子的评论,你应该这样做

let postId='someId'

postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
  });

为了得到特定帖子的评论,你应该这样做

let postId='someId'

postRef=`/posts/${postId}`
firebase.database().ref(postRef).once("value", dataSnapshot => {
comment=dataSnapshot.val().comments
  });

使用以下代码:firebase.database().ref('posts/'+this.state.passKey.).once('value',function(snapshot){child.val().comments})会给我一个错误,“找不到变量:child”,这是因为您使用
child
作为变量,但尚未定义它。所以这可能是一个非常基本的问题,但是我如何定义要在这样的代码中使用的child呢?或者我需要对服务器中的内容使用child吗?使用以下代码:firebase.database().ref('posts/'+this.state.passKey)。一旦('value',函数(snapshot){child.val().comments})给我一个错误,“找不到变量:child”,这是因为您将
child
用作变量,所以这可能是一个非常基本的问题,但是我如何定义要在这样的代码中使用的child呢?或者我需要为服务器中的内容使用child吗?