我的事件文档中有一个名为 title
的字段,其中包含一个 bool 值。我在文档中设置了 onUpdate firestore 触发器。我想如果我的 title
更新了,那么我会做一些 Action 。但如果其他字段被更新,那么我根本不会执行任何操作。怎么做?
如果每次文档更新时都调用下面的函数我没问题,但我只想在 title
更新时做一些进一步的操作
exports.dbEventsOnUpdate = functions.firestore
.document('events/{eventId}').onUpdate(async (change,context) => {
try {
const eventID = context.params.eventId
if (titleIsUpdated) {
return db.doc(``).set(newData)
} else {
// do nothing here
return null
}
} catch(error) {
console.log(error)
return null
}
})
最佳答案
目前,无法根据字段更新触发 Firestore Cloud Functions。它仅在更新文档时触发。
您实际上可以使用以下代码检查标题是否已更新:
const newValue = change.after.data();
const previousValue = change.before.data();
const titleIsUpdated = newValue.title !== previousValue.title;
但请记住,当该文档中的字段发生更改时,您的函数将始终被触发。这可能会产生更多成本,因为 Cloud Functions 根据函数调用收费。 (参见 pricing)
关于firebase - 如何仅在使用 onUpdate 触发器 Firestore 云功能更改字段时触发操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499286/