如何在Firebase的云功能中获取内部子对象? [英] How to get inner child in cloud function for Firebase?
问题描述
这是我的数据库,我想在 PUBLISHED_CONTENT_LIKES
的子级上触发onWrite事件.当我在 publishedContentId1
下添加另一个userId时,可以使用 event.params.pushId
在我的云函数中将contentId标识为 publishedContentId1
.
Here is my database and I want to trigger onWrite event on children of PUBLISHED_CONTENT_LIKES
. When I add another userId under publishedContentId1
, I can identify contentId as publishedContentId1
in my cloud function using 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?
However I want to get newly added userId
as well. How to get that userId using above event?
推荐答案
您可以获取正在 event.data
下写入的数据.要确定新的用户ID,请执行以下操作:
You can get the data that is being written under event.data
. To determine the new user ID:
event.data.val().userID
我建议观看最新的编写数据库函数时的火爆,因为它恰好涵盖了此内容主题.
I recommend watching the latest Firecast on writing Database functions as it covers precisely this topic.
这篇关于如何在Firebase的云功能中获取内部子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!