【发布时间】:2018-01-23 18:51:10
【问题描述】:
我正在使用 Firebase 云函数。我试图在每个帖子上计算喜欢。 我的问题实际上是在 Cloud Functions javascript 代码中。 我正在收听 Firebase 数据库实时中的一个节点,该节点调用“喜欢”。 它看起来像 ->
树是这样保存的:PostID -> UserID -> Data
我想做的是在 Cloud Functions 中获取数据(流派和视频 ID)。
这是代码
exports.countlikechange = functions.database.ref('/likes/{postid}/{userUID}').onWrite(event => {
const collectionRef = event.data.ref.parent;
const model = event.data.val();
console.log("model",model);
const countRef = collectionRef.child('likes');
// Return the promise from countRef.transaction() so our function
// waits for this async event to complete before it exits.
return countRef.transaction(current => {
if (event.data.exists() && !event.data.previous.exists()) {
return (current || 0) + 1;
}
else if (!event.data.exists() && event.data.previous.exists()) {
return (current || 0) - 1;
}
}).then(() => {
console.log('Counter updated.');
});
});
有没有办法从节点获取流派和videoId? 我应该写什么? 例如: const Genre = event.getGenre(类似的东西)
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions