Firestore onUpdate 函数未触发
Firestore onUpdate function not triggering
问题
我在模拟器上使用 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' })
控制台日志 运行s 符合预期,模拟器将其输出到控制台:
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 代码以确保函数已更新
- 我把函数里面的代码都挖空了(如上图),所以我可以确认函数本身不是运行ning
- onUpdate 函数在技术上接受两个参数。两个参数的结果相同。
- 生产中的功能我没试过,只用模拟器试过
相关帖子
我没有在文档选择器中使用任何禁止使用的字符,或者至少我没有收到该错误消息。
使用函数 3.11.0,函数是异步的,因此它们应该隐式地 return Promise。当明确 return 一个值(例如 return 0
)
时,结果是相同的
这是官方文档。据我所知,我正在按照文档所说的进行操作。不过,我可能只是遗漏了一些非常明显的东西。
其他详情
- macOS 大苏尔 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'
问题
我在模拟器上使用 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' })
控制台日志 运行s 符合预期,模拟器将其输出到控制台:
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 代码以确保函数已更新
- 我把函数里面的代码都挖空了(如上图),所以我可以确认函数本身不是运行ning
- onUpdate 函数在技术上接受两个参数。两个参数的结果相同。
- 生产中的功能我没试过,只用模拟器试过
相关帖子
我没有在文档选择器中使用任何禁止使用的字符,或者至少我没有收到该错误消息。
使用函数 3.11.0,函数是异步的,因此它们应该隐式地 return Promise。当明确 return 一个值(例如 return 0
)
这是官方文档。据我所知,我正在按照文档所说的进行操作。不过,我可能只是遗漏了一些非常明显的东西。
其他详情
- macOS 大苏尔 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'