Я написал приведенный ниже код, чтобы определить, когда комментарий был добавлен в БД, а затем ответить, обновив узел временной шкалы, проблема в том, что он фактически не обновляется. Почему это не работает?
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.info(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.info("Success updating this uid comment timeline")
})
.catch((error: string) => {
console.info("Error in catch: "+error)
response.status(500).send(error)
})
return Promise.resolve();
}
Я думал, что это то, что я делаю, как мне вернуть обещание, которое возвращает .set ()?
Я думаю, это из-за твоего const uid = context.params.uid
. Я не вижу имени параметра uid
в вашем ref
.
Это было. @ТораКод
Верните обещание, которое было возвращено set():
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")
return notifTimelineRef.set(newCommentUID)//update
.then(() => {
console.info("Success updating this uid comment timeline")
})
.catch((error: string) => {
console.info("Error in catch: "+error)
response.status(500).send(error)
})
}
ToraCode правильно понял это в своем комментарии, когда сказал:
I think it because of your const uid = context.params.uid. I don't see any param name uid in your ref
Почему вы возвращаете
Promise.resolve()
вместо обещания, которое вернулnotifTimelineRef.set()
? Функция должна знать, что нужно ждать, пока обещание это не будет разрешено.