【问题标题】:How to retrieve parentId when only know childId in Firebase cloud functionsFirebase云函数中仅知道childId时如何检索parentId
【发布时间】:2019-01-31 19:54:55
【问题描述】:

我是 js 和 Firebase 云功能的新手。我在 Firebase 中有一个基本数据结构,每个“posts”和“cmets”都有一个父节点,基本上如下所示:

'posts' > userId > postId

'cmets' > 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 - 我该怎么做呢?

非常感谢!

【问题讨论】:

    标签: javascript firebase cloud


    【解决方案1】:

    这行得通吗?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多