【发布时间】:2019-10-15 16:56:18
【问题描述】:
我编写了下面的代码来检测何时将评论添加到数据库中,然后通过更新时间线节点进行响应,问题是它实际上并没有更新。 为什么它不起作用?
export const onCommentAdded = functions.database
.ref('/Comments/{receiverUID}/{postID}/{mediaNum}/{commentID}')
.onCreate((snapshot, context) => {
const uid = context.params.uid
const newCommentUID = snapshot.child("UID").val()
console.log(newCommentUID, " the comment")
return addNewCommentNotif(uid, newCommentUID)
})
function addNewCommentNotif(uuid: string, newCommentUID: string) {
//NotifTimeline/uid/NewNotif (someuniqueVal)/commentID
const randID = Math.floor(100000000 + Math.random() * 900000000);
const notifTimelineRef = admin.database().ref("NotifTimeline").child(uuid).child(newCommentUID + ":" + randID).child("NewComment")
notifTimelineRef.set(newCommentUID)//update
.then(() => {
console.log("Success updating this uid comment timeline")
})
.catch((error: string) => {
console.log("Error in catch: "+error)
response.status(500).send(error)
})
return Promise.resolve();
}
【问题讨论】:
-
你为什么返回
Promise.resolve()而不是notifTimelineRef.set()返回的promise?该函数需要知道等到 那个 承诺被解决。 -
我以为这就是我正在做的事情,我如何返回由 .set() 返回的承诺?
-
我想是因为你的
const uid = context.params.uid。我在您的ref中没有看到任何参数名称uid。 -
就是这样。 @ToraCode
标签: typescript firebase firebase-realtime-database google-cloud-functions