[英]Cloud function for firestore onWrite
当我通过 firebase 控制台更新或删除我的文档时,我的 function 被触发但是当我记录字段context.eventType
时,我得到的是google.firestore.document.write
而不是google.firestore.document.delete
或google.firestore.document.update
请问有什么线索吗?
代码:
exports.useTest = functions.firestore
.document("users/{userId}")
.onWrite(async (handle, context) => {
console.log("before", handle.before.data());
console.log("after", handle.after.data());
console.log("eventType", context.eventType);
});
根据此文档,您的 Firestore 函数将触发onWrite
,因为无论是否触发onCreate
、 onUpdate
或onDelete
都会触发它。 无论触发器如何,都会显示onWrite
。
还有一个指南可以触发您可以遵循的文档的所有更改的功能:
exports.modifyUser = functions.firestore
.document('users/{userID}')
.onWrite((change, context) => {
// Get an object with the current document value.
// If the document does not exist, it has been deleted.
const document = change.after.data();
// Get an object with the previous document value (for update or delete)
const oldDocument = change.before.data();
// perform desired operations ...
});
如需进一步参考,您可以参考以下文档:
您可以检查handle
:
if (!handle.before.exists && handle.after.exists) {
// created
} else if (handle.before.exists && !handle.after.exists) {
// deleted
} else {
// updated
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.