【问题标题】:How to get inner child in cloud function for Firebase?如何在 Firebase 的云功能中获取内部孩子?
【发布时间】:2017-09-15 10:52:00
【问题描述】:

这是我的数据库,我想在PUBLISHED_CONTENT_LIKES 的孩子上触发 onWrite 事件。当我在publishedContentId1 下添加另一个userId 时,我可以在我的云函数中使用event.params.pushId 将contentId 标识为publishedContentId1

exports.handleLikeEvent = functions.database.ref('/USER_MANAGEMENT/PUBLISHED_CONTENT_LIKES/{pushId}')
.onWrite(event => {

  // Grab the current value of what was written to the Realtime Database.
  //const userId = event.data.child(publishedContentId);
  //const test = event.params.val();
  const publishedContentId = event.params.pushId;

  var result = {"publishedContentId" : "saw"}
  // You must return a Promise when performing asynchronous tasks inside a Functions such as
  // writing to the Firebase Realtime Database.
  // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
  return event.data.ref.parent.parent.child('PUBLISHED_CONTENTS/'+publishedContentId).set(result);
});

但是我也想获得新添加的userId。如何使用上述事件获取该 userId?

【问题讨论】:

    标签: node.js firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    你可以得到event.data下正在写入的数据。要确定新的用户 ID:

    event.data.val().userID
    

    我建议观看最新的Firecast on writing Database functions,因为它恰好涵盖了这个主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2019-02-22
      • 2019-10-17
      • 2020-12-28
      • 2020-02-28
      • 2017-10-14
      • 2018-03-12
      • 2017-11-26
      相关资源
      最近更新 更多