如何解决Firestore onUpdate 函数未触发 问题调试步骤相关帖子其他细节
问题
我在模拟器中使用 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
)
这是官方文档。据我所知,我正在按照文档说的做。不过,我可能只是遗漏了一些非常明显的东西。
其他细节
- macOS Big Sur 11.1 (20C69)
- Firebase CLI 9.1.0
模拟器应该是最新的
有什么想法吗?谢谢!
解决方法
结果我忘了在 index.ts 函数文件中导入 onMusicUpdated
。因此模拟器永远不知道它的存在!
改变了
// functions/src/index.ts
export { onMusicCreated } from './music'
到
// functions/src/index.ts
export { onMusicCreated,onMusicUpdated } from './music'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。