A Firestore trigger function is a special cloud function that runs automatically when data changes in a Firestore database, like when a document is created, updated, or deleted.
Click to reveal answer
beginner
Name the three main Firestore trigger event types.
The three main Firestore trigger event types are: 1. onCreate - runs when a new document is added. 2. onUpdate - runs when an existing document is changed. 3. onDelete - runs when a document is removed.
Click to reveal answer
intermediate
How do you access the changed data in an onUpdate Firestore trigger?
In an onUpdate trigger, you get two snapshots: before and after. Use change.before.data() to see old data and change.after.data() to see new data.
Click to reveal answer
intermediate
Why use Firestore trigger functions instead of client-side code?
Trigger functions run on the server, so they are secure and reliable. They can enforce rules, update other data, or send notifications without depending on the user's device or app.
Click to reveal answer
beginner
What is the syntax to create a Firestore onCreate trigger for a collection named 'users'?
B. onDelete trigger cannot be used on 'users/{userId}' path
C. snap.data should be snap.data() to get document data
D. context parameter is not allowed in onDelete triggers
Solution
Step 1: Check how to access document data
Firestore document snapshot data is accessed by calling snap.data(), not snap.data property.
Step 2: Identify the error in code
The code uses snap.data without parentheses, causing undefined userData and runtime error.
Final Answer:
snap.data should be snap.data() to get document data -> Option C
Quick Check:
Use snap.data() method to access document data [OK]
Hint: Use snap.data() method, not snap.data property [OK]
Common Mistakes:
Using snap.data instead of snap.data()
Thinking onDelete can't use context parameter
Assuming async is always required
5. You want to create a Firestore trigger that updates a user's profile summary whenever their profile document changes. Which approach correctly ensures the trigger only runs when the 'bio' field changes, avoiding infinite loops?
hard
A. Use onUpdate trigger and compare change.before.data().bio with change.after.data().bio before updating
B. Use onWrite trigger and update the document unconditionally inside the trigger
C. Use onCreate trigger to update bio field after document creation
D. Use onUpdate trigger and update the document without checking any field changes
Solution
Step 1: Choose the right trigger for updates
onUpdate triggers run on any document update, suitable for detecting changes.
Step 2: Prevent infinite loops by checking field changes
Compare bio field before and after update; only proceed if bio changed to avoid recursive updates.
Final Answer:
Use onUpdate trigger and compare change.before.data().bio with change.after.data().bio before updating -> Option A
Quick Check:
Check field changes in onUpdate to avoid loops [OK]
Hint: Compare before and after fields in onUpdate to avoid loops [OK]
Common Mistakes:
Using onWrite and updating unconditionally causing loops
Using onCreate which triggers only once
Not checking field changes before updating
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.