【问题标题】:onUpdate cloud function is not working with firestore dataonUpdate 云功能不适用于 Firestore 数据
【发布时间】:2021-11-18 22:17:06
【问题描述】:

我有一个 firestore 集合,其中有一个类似于 category 的字段,其中包含很少的值,例如类别 1、类别 2。我有一个云功能,可以让我获得类别 1 和类别 2 的总数和持续时间。

这个函数在创建时工作得很好。但是当我更新它时,计数正在更新,但持续时间没有。这是我的更新功能;

export const updateDuration = 
functions.firestore.document('Duration/{userId}/Duration/{uid}')
.onUpdate(async(change, context) => {
const oldData = change.before.data();

const newData = change.after.data();

const Duration = newData.WorkDuration;

if (newData.WorkDuration == oldData.WorkDuration) {
   return null } else {
    const Ref = newData.parent!
    Ref.set({
     Duration: Duration
    })
    
   }

   return null

  


})

【问题讨论】:

  • 嗨,你能告诉我们这个持续时间到底是做什么的吗?它是收藏中的一个领域还是衡量某些东西?如果可能的话,您能否也请与我们分享您的数据库结构?

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

return null 可能在 set() 承诺解决之前终止函数。试试这个:

export const updateDuration = 
functions.firestore.document('Duration/{userId}/Duration/{uid}')
.onUpdate(async (change, context) => {
  const oldData = change.before.data();
  const newData = change.after.data();

  const Duration = newData.WorkDuration;

  if (newData.WorkDuration == oldData.WorkDuration) {
    return null 
  } else {
    const Ref = newData.parent!
    return Ref.set({ // add return here
      Duration: Duration
    }) 
  }
})

【讨论】:

  • 谢谢,但是当我这样做时,我在部署函数时出错 错误 TS7030: 并非所有代码路径都返回一个值。
猜你喜欢
  • 2020-03-28
  • 2020-05-28
  • 2021-05-11
  • 2021-04-08
  • 1970-01-01
  • 2020-08-02
  • 2018-06-30
  • 2021-07-07
  • 1970-01-01
相关资源
最近更新 更多