firebase - 如何仅在使用 onUpdate 触发器 Firestore 云功能更改字段时触发操作?

标签 firebase google-cloud-firestore google-cloud-functions

我的事件文档中有一个名为 title 的字段,其中包含一个 bool 值。我在文档中设置了 onUpdate firestore 触发器。我想如果我的 title 更新了,那么我会做一些 Action 。但如果其他字段被更新,那么我根本不会执行任何操作。怎么做?

如果每次文档更新时都调用下面的函数我没问题,但我只想在 title 更新时做一些进一步的操作

exports.dbEventsOnUpdate = functions.firestore
.document('events/{eventId}').onUpdate(async (change,context) => {

        try {        
            const eventID = context.params.eventId

            if (titleIsUpdated) {
               return db.doc(``).set(newData)
            } else {
               // do nothing here
               return null
            }

        } catch(error) {
            console.log(error)
            return null
        }

    })

最佳答案

目前,无法根据字段更新触发 Firestore Cloud Functions。它仅在更新文档时触发。

您实际上可以使用以下代码检查标题是否已更新:

const newValue = change.after.data();
const previousValue = change.before.data();

const titleIsUpdated = newValue.title !== previousValue.title;

但请记住,当该文档中的字段发生更改时,您的函数将始终被触发。这可能会产生更多成本,因为 Cloud Functions 根据函数调用收费。 (参见 pricing)

关于firebase - 如何仅在使用 onUpdate 触发器 Firestore 云功能更改字段时触发操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59499286/

相关文章:

java - 在 documentSnapShot 中获取值,但将其转换为对象时,对象返回 null,为什么?

firebase - Firestore 安全规则 : Where's hasOnly function?

firebase - Firebase 是云功能吗?术语 2gen 和 v2 指的是同一个东西吗?

firebase - Firebase Cloud Functions 可以将 OAuth2 与 gmail 一起使用吗?

android - Firebase 搜索事件术语未显示在分析中

javascript - 在 Firebase 中将捕捉值分配给变量

node.js - 使用 Cloud Functions for Firebase 和 @google-cloud/storage 删除图像时出现问题

node.js - DeprecationWarning : grpc. 加载,改用@grpc/proto-loader

firebase - Firebase 身份验证自定义声明和没有写入权限的 Firestore 文档有什么区别?

firebase - 如何将用户密码传递给 Firebase 可调用函数内的 puppeteer?