簡體   English   中英

用於 firestore onWrite 的雲 function

[英]Cloud function for firestore onWrite

當我通過 firebase 控制台更新或刪除我的文檔時,我的 function 被觸發但是當我記錄字段context.eventType時,我得到的是google.firestore.document.write而不是google.firestore.document.deletegoogle.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 ,因為無論是否觸發onCreateonUpdateonDelete都會觸發它。 無論觸發器如何,都會顯示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 ...
    });

如需進一步參考,您可以參考以下文檔:

  1. 創建新文檔時觸發函數
  2. 更新文檔時觸發函數
  3. 刪除文檔時觸發函數

您可以檢查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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM