Reputation: 23
How can I implement firestore database trigger (onCreate, onUpdate) with Cloud functions for firebase gen 2 ?
As in Firestore cloud function gen1 we can use Cloud firestore database triggers, But how can we implement this in Firestore cloud function gen 2
Upvotes: 1
Views: 1137
Reputation: 164
I am successfully listening for events from Firestore v2
via Gen 2 Functions
. You can give something like below a try in your index.js
if using Node.js Functions.
const {initializeApp} = require("firebase-admin/app");
const {onDocumentUpdated} = require("firebase-functions/v2/firestore");
initializeApp();
exports.myUpdateFunc =
onDocumentUpdated('/myCollectionName/{myDocId}', (event) => {
const old_doc = event.data.before.data();
const new_doc = event.data.after.data();
const id = event.params.myDocId;
// do something
});
Upvotes: 1
Reputation: 1809
Firestore Triggers for Cloud Functions 2nd Gen are now in Public Preview.
You can view the documentation here: https://firebase.google.com/docs/firestore/extend-with-functions-2nd-gen
Upvotes: 2
Reputation: 50930
As mentioned in the documentation, Cloud functions gen 2 do not support Cloud Firestore triggers at the moment.
You'll have to use Gen 1 functions if your application rely on Firestore triggers.
Upvotes: 3