在修改后的文档上触发电子邮件 Firebase 扩展

Trigger Email Firebase Extension on modified Document

如何在修改文档数据时触发发送电子邮件?

Trigger Email 仅根据写入 到 Cloud Firestore 集合但未修改

的文档内容撰写和发送电子邮件

我搞不懂这个……

通过检查 code 我们可以看到扩展程序已经处理了对文档的所有写入:

export const processQueue = functions.handler.firestore.document.onWrite(
  ...

稍微看一下 further 扩展在写入时采取的操作似乎取决于文档中 delivery.state 字段的值。

async function processWrite(change) {
  ...
  const payload = change.after.data() as QueuePayload;
  ...

  switch (payload.delivery.state) {
    case "SUCCESS":
    case "ERROR":
      return null;
    case "PROCESSING":
      if (payload.delivery.leaseExpireTime.toMillis() < Date.now()) {
        // Wrapping in transaction to allow for automatic retries (#48)
        return admin.firestore().runTransaction((transaction) => {
          transaction.update(change.after.ref, {
            "delivery.state": "ERROR",
            error: "Message processing lease expired.",
          });
          return Promise.resolve();
        });
      }
      return null;
    case "PENDING":
    case "RETRY":
      // Wrapping in transaction to allow for automatic retries (#48)
      await admin.firestore().runTransaction((transaction) => {
        transaction.update(change.after.ref, {
          "delivery.state": "PROCESSING",
          "delivery.leaseExpireTime": admin.firestore.Timestamp.fromMillis(
            Date.now() + 60000
          ),
        });
        return Promise.resolve();
      });
      return deliver(payload, change.after.ref);
  }
}

我的猜测是,如果您清除该字段,分机将接受该更改并尝试再次邮寄文档。