[英]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.