【发布时间】:2018-07-27 14:09:14
【问题描述】:
我正在使用云函数来计算帖子中有多少 cmets。 当我添加评论时,它会将其保存在 firebase 数据库中,然后在 Cloud 函数中,有一个函数可以侦听“Comments”节点并且应该“+1”返回到 firebase 数据库。
由于某种原因,它仅在我从 firebase 数据库中删除评论时才有效。 当我删除评论时,添加“+1”。
这是我的代码
exports.commentsCount = functions.database.ref('/comments/{commentid}/{userUID}').onWrite(event =>{
const collectionRef = event.data.ref.parent;
const model = event.data.previous.val();
const commentid = event.params.commentid;
console.log("commentID:",commentid);
const countComments = collectionRef.child('countComments');
return countComments.transaction(current => {
console.log('Before the If');
if (!event.data.exists() && event.data.previous.exists()) {
console.log('Enter to the if.');
const commentsList = admin.database().ref(`comments/${commentid}/countComments`).transaction(current => {
return (current || 0) + 1;
});
}
}).then(() => {
console.log('Comments counter updated.');
});
});
谁能告诉我我哪里做错了?
【问题讨论】:
标签: javascript firebase firebase-realtime-database google-cloud-functions