I would like to trigger a cloud function only when a particular field of a firestore document is updated. I don't want to trigger it every time the document is updated regardless which field. Is it possible?
For example:
exports.onAvatarUpdated = functions.firestore.document('users/{userId}/avatarUrl')
.onUpdate(change => {
// do something here
});
Currently there are no triggers for a specific field. You can only trigger on any change to a document, and if you want to know if a particular field changed, you have to examine the change object to figure it out.
You can always file a feature request if you would like to be able to express a function that only pays attention to particular field.
I will change Firebase native manual code from here:
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = change.after.data();
const newFieldValue = newValue.field;
// ...or the previous value before this update
const previousValue = change.before.data();
const previousFieldValue = previousValue.field;
if (previousFieldValue!== newFieldValue)
{ // perform desired operations ... }
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With