【发布时间】:2017-10-11 05:06:04
【问题描述】:
我想实现一个计数器函数来增加一个帖子的点赞数,这里是代码(取自 firebase 计数器函数示例)我已经修改了一点,我如何获得指定的占位符值数据库参考(cid,coid)?
exports.countCommentChange = functions.database.ref('/clipComments/{cid}/{coid}').onWrite(event => {
const db = admin.database();
const clipRef = db.ref(`/clips/${cid}`); // <- how do I get CID?
const countRef = clipRef.child('comments');
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.');
});
});
我的数据库结构如下:
clips: {
clipId: {
name: "my awesome clip",
likes: 0,
comments: 0
}
},
clipComments: {
clipId:
{
commentTimestamp: {
comment: "awesome video!"
}
}
}
【问题讨论】:
标签: node.js firebase firebase-realtime-database