I want Cloud Function trigger only when document is create and update. I do not want Cloud Function trigger onDelete
.
Is there any trigger for only onCreate
and onUpdate
without make 2 separate function?
I see Firebase have onWrite
, but this trigger also onDelete
:
onCreate Triggered when a document is written to for the first time.
onUpdate Triggered when a document already exists and has any value
changed. onDelete Triggered when a document with data is deleted.
onWrite Triggered when onCreate, onUpdate or onDelete is triggered.
Thanks everyone!
You can use onWrite listener with handler that does nothing in case of deletion.
exports.myFunction = functions.firestore
.document('/abc/{docId}')
.onWrite((change, context) => {
// Exit when the data is deleted.
if (!change.after.exists) {
return null;
}
//TODO: put code here you want to execute for create and update
});
There are no combination triggers, so you will need to declare two Cloud Functions for this.
But you can implement your actual logic in a single regular function, and then just call that from the two Cloud Functions.
Something like
exports.createUser = functions.firestore
.document('users/{userId}')
.onCreate((snap, context) => {
doTheThing(snap, context);
});
exports.updateUser = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
doTheThing(change.after, context);
});
function doTheThing(snapshot, context) {
...
};
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