如何让Firebase的云端功能成为内心孩子?

时间:2017-04-19 09:36:39

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

enter image description here

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

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?

1 个答案:

答案 0 :(得分:1)

您可以在event.data下获取正在撰写的数据。要确定新用户ID:

event.data.val().userID

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