user11214880
user11214880

Reputation:

Firebase cloud function not running?

I have written the bellow code to detect when a comment has been added to the DB and then to respond by updating a timeline node the issue is that it does not actually update. Why is it not working?

    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();
}

Upvotes: 0

Views: 180

Answers (2)

user11214880
user11214880

Reputation:

ToraCode got it right in his comment when he said:

I think it because of your const uid = context.params.uid. I don't see any param name uid in your ref

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317352

Return the promise that was returned by 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.log("Success updating this uid comment timeline")
  })
  .catch((error: string) => {
    console.log("Error in catch: "+error)
    response.status(500).send(error)
  })
}

Upvotes: 0

Related Questions