0
我想实现一个计数器函数来合计一个帖子上的喜欢数,这里是代码(取自firebase计数器函数示例)我已经修改了一下,怎么做我得到数据库ref(cid,coid)中指定的占位符值?在firebase上获取占位符值onWrite数据库函数
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!"
}
}
}
这证明是正确的答案,谢谢! –