I’m trying to migrate to Could Functions 2nd gen. Here is what I have tried:
import {
onDocumentWritten,
Change,
FirestoreEvent
} from "firebase-functions/v2/firestore";
exports.myfunction = onDocumentWritten("users/{userId}", (event) => {
console.log(event.data.after.data());
});
I following the docs but there is nothing mentioned about how to know what kind of event happened when using onDocumentWritten
? Is it created, updated or deleted?
2
Answers
You can test some part of the event to know the type of event
I had written this code which uses the same concept as samthcodingman, so this is how it would look. As mentioned, for all three events.
Javascript
So, using the change object. If change.before is null, it’s a create. if change.after is null, it’s a delete. Otherwise, it’s an update operation.