Learn to use Firestore triggers in Firebase Functions. Our step-by-step guide covers project setup, CLI installation, function creation, and deployment.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Firebase Project
To use Firestore triggers in Firebase functions, you need to set up a Firebase project. Follow these steps:
Step 2: Install Firebase CLI
You will need the Firebase CLI to deploy your functions. Install it if you haven't already:
npm install -g firebase-tools
Step 3: Initialize Firebase Functions
Run the following command to initialize Firebase Functions in your project directory:
firebase init functions
Step 4: Define Firestore Trigger Function
Open the functions/index.js file (or functions/src/index.ts for TypeScript) and define a Firestore trigger like this:
const functions = require('firebase-functions');
exports.onDocumentCreate = functions.firestore
.document('/collectionName/{documentId}')
.onCreate((snap, context) => {
const newValue = snap.data();
// Perform desired operations on newValue
console.log('New document created:', newValue);
return null;
});
Replace /collectionName/{documentId} with the path of the document you want to watch.
Step 5: Add Firestore Trigger Logic
Depending on your use case, you can add logic to the trigger function. For example, you might want to send a notification or update another document. Here's an example of sending a message when a document is created:
exports.onDocumentCreate = functions.firestore
.document('/collectionName/{documentId}')
.onCreate((snap, context) => {
const newValue = snap.data();
// Example logic: Log a message when a new document is created
const message = `A new document with ID ${context.params.documentId} was created.`;
console.log(message);
// Return null or a Promise
return null;
});
Step 6: Deploy Firebase Functions
Run the following command to deploy your function:
firebase deploy --only functions
This command will deploy your function to Firebase, and it will start executing when a document is created in the specified Firestore collection.
Step 7: Test the Firestore Trigger
To test your Firestore trigger, add a new document to the specified collection in Firestore. You can do this manually through the Firebase Console or use a script to add data. Check the Firebase Functions logs to verify if your trigger executed successfully:
firebase functions:log
This command will show you the logs from your function execution, and you can see the output from your console.log statements.
Step 8: Modify Firestore Trigger for Other Operations
You can create triggers for other operations like update, delete, and write (any operation). Here's an example for an update trigger:
exports.onDocumentUpdate = functions.firestore
.document('/collectionName/{documentId}')
.onUpdate((change, context) => {
const newValue = change.after.data();
const previousValue = change.before.data();
// Compare values, perform operations
console.log('Document updated:', newValue, 'Previous Value:', previousValue);
return null;
});
Modify /collectionName/{documentId} and adapt the logic to fit your use case.
Conclusion:
By following these steps, you have set up and deployed Firestore triggers in Firebase functions. These triggers allow you to automate backend operations in response to changes in your Firestore database. Remember to adapt the code examples to your specific project requirements.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.