【问题标题】:Firestore onUpdate function not triggeringFirestore onUpdate 功能未触发
【发布时间】:2021-04-08 13:32:21
【问题描述】:

问题

我在模拟器中使用 Firebase 云函数,由于某种原因,onUpdate 触发器未触发,但 onCreate 函数确实触发了。

所有代码都是 TypeScript,并被转译为 JS 以用于云功能。

// functions/src/music.ts

// this function runs
export const onMusicCreated = functions.firestore
    .document('music/{musicId}')
    .onCreate(async (snapshot) => {
        console.log('on create is running')
    })

// this function doesn't run
export const onMusicUpdated = functions.firestore
    .document('music/{musicId}')
    .onUpdate(async (change) => {
        console.log('on update is running')
    })

这两个函数都是异步的,因为在最终代码中,

在前端,当我在前端运行add 函数时,onCreate 函数会触发。

const { id } = await firebase.firestore().collection('music').add({ title: 'hello world' })

控制台日志按预期运行,模拟器将其输出到控制台:

i  functions: Beginning execution of "onMusicCreated"
i  functions: Finished "onMusicCreated" in ~1s

然而,当我更新同一个文档时,onUpdate 函数没有运行。

// "id" is the same id as above
await firebase.firestore().doc(`music/${id}`).update({ title: 'new title' })

什么都没有发生。当我查看 firestore 模拟器时,我可以确认文档实际上已更新。我错过了什么明显的东西吗?前端代码相比我的实际使用进行了简化,但功能代码一点都没有简化。我可以确认 firestore 文档已按预期创建和更新。

控制台或日志中没有错误消息。

调试步骤

  • 我已检查以确保函数代码已正确转换为 JS。我既查看了代码输出,又多次更新了 onCreate 代码以确保函数更新
  • 我把函数内部的所有代码都挖空了(如上图),所以我可以确认函数本身没有运行
  • onUpdate 函数在技术上接受两个参数。两个参数的结果相同。
  • 我没有尝试过生产中的功能,只用模拟器。

相关帖子

Why doesn't firestore onWrite trigger get invoked on firebase cloud functions emulator?

我没有在文档选择器中使用任何禁止字符,或者至少我没有收到该错误消息。

Firebase Cloud Functions for Firestore are not triggering

使用函数 3.11.0,并且函数是异步的,因此它们应该隐式返回 Promise。显式返回值时结果相同(例如,return 0

https://firebase.google.com/docs/functions/firestore-events#trigger_a_function_when_a_document_is_updated

这是官方文档。据我所知,我正在做文档所说的。不过,我可能只是遗漏了一些非常明显的东西。

其他详情

  • macOS Big Sur 11.1 (20C69)
  • Firebase CLI 9.1.0

模拟器应该是最新的

有什么想法吗?谢谢!

【问题讨论】:

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


    【解决方案1】:

    原来我只是忘记在 index.ts 函数文件中导入 onMusicUpdated。因此模拟器从来不知道它的存在!

    改变了

    // functions/src/index.ts
    
    export { onMusicCreated } from './music'
    

    // functions/src/index.ts
    
    export { onMusicCreated, onMusicUpdated } from './music'
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2021-09-03
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多