【问题标题】:Firebase cloud function not running?Firebase 云功能未运行?
【发布时间】: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


【解决方案1】:

ToraCode 在他的评论中说得对:

我认为是因为您的 const uid = context.params.uid。我在您的参考文献中没有看到任何参数名称 uid

【讨论】:

    【解决方案2】:

    返回 set() 返回的 promise:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      • 2021-10-26
      • 1970-01-01
      • 2021-11-27
      • 2017-08-03
      相关资源
      最近更新 更多