在Firebase云功能中仅知道childId时如何检索parentId

时间:2018-08-25 17:26:48

标签: javascript firebase cloud

我是js和Firebase云功能的新手。我在Firebase中有一个基本的数据结构,每个父节点都有一个“帖子”和“评论”,基本上像这样:

“帖子”> userId> postId

“评论”> postId> commentId

只要有新注释为我提供postId和commentId,我就有一种方法可以观察。该方法如下所示:

exports.observeComment = functions.database.ref('/comments/{postId}/{commentId}').onCreate((snapshot, context) => {
  var postId = context.params.postId;
  var commentId = context.params.commentId;
  console.log('LOGGER --- New comment, postId: ' + postId + ' , commentId: ' + commentId);


  return admin.database().ref().child('/posts/').once('value', function(snapshot) {

    //Get userId

  })
})

我现在想检索postId的userId-我该怎么做?

非常感谢!

1 个答案:

答案 0 :(得分:0)

这项工作可以吗?

return admin.database().ref().child('/posts/').orderByChild('postId').equalTo(postId).on('value', function(snapshot) {
    //Get userId
    snapshot.forEach((function(child) { console.log(child.userId) });
});