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

问题描述 投票:0回答:1

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

我可以,如果每次文档更新时都调用下面的函数,但是仅当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
        }

    })
firebase google-cloud-firestore google-cloud-functions
1个回答
1
投票

当前,无法基于字段更新来触发Firestore Cloud Functions。仅在文档更新时触发。

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

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

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

但是请记住,在该文档中更改字段时,总是会触发您的函数。这可能会产生更多成本,因为Cloud Functions是基于函数调用收费的。 (请参见pricing

© www.soinside.com 2019 - 2024. All rights reserved.