0
0
Firebasecloud~10 mins

Firestore trigger functions in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Firestore trigger functions
Event in Firestore
Trigger Function Activated
Read Event Data
Perform Logic (e.g., update, notify)
Write Back or Side Effects
Function Ends
When a Firestore event happens, the trigger function runs, reads event data, does work, then finishes.
Execution Sample
Firebase
exports.onUserCreate = functions.firestore
  .document('users/{userId}')
  .onCreate((snap, context) => {
    const newUser = snap.data();
    console.log('New user:', newUser.name);
  });
This function runs when a new user document is created and logs the user's name.
Process Table
StepEventTrigger Function ActionData AccessedOutput/Effect
1New document created in 'users/abc123'Function onUserCreate activatessnap.data() returns {name: 'Alice'}Logs: 'New user: Alice'
2Function completesNo further actionNo new dataFunction ends
💡 Function ends after processing the create event and logging user name
Status Tracker
VariableStartAfter Step 1Final
snapundefinedDocumentSnapshotDocumentSnapshot
newUserundefined{name: 'Alice'}{name: 'Alice'}
Key Moments - 2 Insights
Why does the function only run when a new document is created?
Because the trigger uses .onCreate(), it activates only on document creation events as shown in execution_table step 1.
What is snap.data() and why do we use it?
snap.data() gives the content of the changed document. In step 1, it returns the new user's data so we can use it inside the function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what data does snap.data() return at step 1?
Aundefined
B{name: 'Alice'}
C{userId: 'abc123'}
Dnull
💡 Hint
Check the 'Data Accessed' column in execution_table row for step 1
At which step does the function finish running?
AStep 1
BBefore Step 1
CStep 2
DNever finishes
💡 Hint
Look at the 'Output/Effect' column in execution_table for step 2
If the trigger was changed to .onUpdate(), when would the function run?
AWhen a document is updated
BWhen a document is deleted
CWhen a document is created
DWhen Firestore starts
💡 Hint
Recall the meaning of .onUpdate() triggers in Firestore functions
Concept Snapshot
Firestore trigger functions run automatically on database events.
Use .onCreate(), .onUpdate(), or .onDelete() to specify event type.
Function receives snapshot with event data.
Perform logic inside function, then finish.
Useful for reacting to data changes in real time.
Full Transcript
Firestore trigger functions activate automatically when specific events happen in your Firestore database, like creating, updating, or deleting documents. For example, a function with .onCreate() runs only when a new document is added. Inside the function, you get a snapshot of the changed document's data using snap.data(). You can then perform actions like logging, updating other data, or sending notifications. After the function finishes its work, it ends. This lets your app respond instantly to database changes without manual checks.