仅当使用onUpdate触发器Firestore云功能更改字段时,如何使操作触发? [英] how to make an action triggered only if a field is changed using onUpdate trigger Firestore cloud function?

查看:61
本文介绍了仅当使用onUpdate触发器Firestore云功能更改字段时,如何使操作触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的事件文档中有一个名为title的字段,其中包含一个布尔值.我在文档中设置了onUpdate Firestore触发器.我想如果我的title被更新,那么我将做一些动作.但是如果其他字段已更新,则我将完全不执行任何操作.该怎么做?

There is a field in my event document called title which contains a boolean value. I have set onUpdate firestore trigger in my document. I want if my title is updated then I will do some action. but if other fields is updated then I will not perform any action at all. how to do that ?

如果在文档上有一次更新时调用下面的函数,我可以,但是我只想在title被更新时才做一些进一步的动作

I am okay if the function below is invoked every time there is an update on the document, but I want only do some further action only if the title is updated

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.仅在文档更新时触发.

Currently, Firestore Cloud Functions cannot be triggered based on a field update. It's only triggered when a document is updated.

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

You can actually check if the title was updated using the following code:

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

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

但是请记住,在该文档中更改字段时,总是会触发您的函数.这可能会招致更多成本,因为Cloud Functions是基于函数调用来收费的. (请参见定价)

But keep in mind that your function will always be triggered when a field is changed in that document. And this might incur more costs, since Cloud Functions charge based on functions invocations. (See pricing)

这篇关于仅当使用onUpdate触发器Firestore云功能更改字段时,如何使操作触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆