【发布时间】:2019-08-04 00:44:18
【问题描述】:
要求的行为:
我想在打字稿中创建一个云函数,每次将文档添加到帖子集合的 cmets 子集合时都会执行该函数。
执行应将父文档上的计数器加一。
当前状态
如果我用 console.log() 语句替换“get promise”,则每次创建文档时都会执行云函数。
问题
它不执行更新部分。相反,它会引发错误:
Object is possibly 'undefined'
解决方法
我在不同的云功能上遇到了类似的问题,并使用了 if 语句来解决它。但是,我不明白如何在这里应用它。
我该如何解决这个问题?我必须使用 if 语句吗?
export const createSubCollTrigger =
functions.firestore.document('posts/{postID}/comments/{commentID}').onCreate((snap, context) => {
admin.firestore().doc('posts/{postID}').get()
.then(snapshot => {
const data = snapshot.data()
return admin.firestore().doc('posts/{postID}').update({postCommentsTot: data.postCommentsTot + 1});
})
.catch(error => {
console.log(error)
return
})
})
**
【问题讨论】:
-
感谢您粘贴代码。没有必要显示任何代码截图。指出第 38 行是什么,错误消息告诉您存在问题的位置会更有帮助。
标签: javascript typescript firebase google-cloud-firestore google-cloud-functions