简体   繁体   English

用于 firestore onWrite 的云 function

[英]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.deletegoogle.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 ,因为无论是否触发onCreateonUpdateonDelete都会触发它。 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:如需进一步参考,您可以参考以下文档:

  1. Trigger a function when a new document is created 创建新文档时触发函数
  2. Trigger a function when a document is updated 更新文档时触发函数
  3. Trigger a function when a document is deleted 删除文档时触发函数

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM