[英]Cloud function for firestore onWrite
When I update or delete my document via firebase console my function triggered but when I log the field context.eventType
I'm getting google.firestore.document.write
instead of google.firestore.document.delete
or google.firestore.document.update
当我通过 firebase 控制台更新或删除我的文档时,我的 function 被触发但是当我记录字段
context.eventType
时,我得到的是google.firestore.document.write
而不是google.firestore.document.delete
或google.firestore.document.update
Any clue please?请问有什么线索吗?
Code:代码:
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);
});
According to this documentation , your Firestore functions will trigger the onWrite
since it is triggered whether onCreate
, onUpdate
or onDelete
is triggered.根据此文档,您的 Firestore 函数将触发
onWrite
,因为无论是否触发onCreate
、 onUpdate
或onDelete
都会触发它。 Regardless of the trigger, the onWrite
will be shown.无论触发器如何,都会显示
onWrite
。
There's also a guide to trigger a function for all changes to a document that you can follow:还有一个指南可以触发您可以遵循的文档的所有更改的功能:
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 ...
});
For further reference, you can refer to these documentations:如需进一步参考,您可以参考以下文档:
You can inspect handle
:您可以检查
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.