Challenge - 5 Problems
Firestore Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does this Firestore trigger function log?
Consider this Firestore trigger function that runs when a document is created in the 'users' collection:
What will be logged if a new user document with {email: 'alice@example.com'} is added?
exports.logNewUser = functions.firestore.document('users/{userId}').onCreate((snap, context) => { const newUser = snap.data(); console.log('New user email:', newUser.email); return null;});What will be logged if a new user document with {email: 'alice@example.com'} is added?
Firebase
exports.logNewUser = functions.firestore.document('users/{userId}').onCreate((snap, context) => { const newUser = snap.data(); console.log('New user email:', newUser.email); return null; });
Attempts:
2 left
💡 Hint
Remember that snap.data() returns the document data when a document is created.
✗ Incorrect
The onCreate trigger receives a snapshot of the new document. Calling snap.data() returns the document's fields. The console.log prints the email field from the new user document.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Firestore trigger
Which option contains the correct syntax for a Firestore onUpdate trigger on the 'orders' collection?
Attempts:
2 left
💡 Hint
Check the arrow function syntax and parentheses.
✗ Incorrect
Option A correctly uses an arrow function with parentheses around parameters and braces around the function body. Other options have missing parentheses or braces causing syntax errors.
❓ optimization
advanced3:00remaining
Optimize this Firestore trigger to reduce reads
This Firestore onWrite trigger reads the same document twice:
Which option optimizes the code to avoid duplicate reads?
exports.updateStats = functions.firestore.document('posts/{postId}').onWrite(async (change, context) => { const postId = context.params.postId; const doc1 = await admin.firestore().collection('posts').doc(postId).get(); const doc2 = await admin.firestore().collection('posts').doc(postId).get(); // ... use doc1 and doc2 ... return null;});Which option optimizes the code to avoid duplicate reads?
Firebase
exports.updateStats = functions.firestore.document('posts/{postId}').onWrite(async (change, context) => { const postId = context.params.postId; const doc1 = await admin.firestore().collection('posts').doc(postId).get(); const doc2 = await admin.firestore().collection('posts').doc(postId).get(); // ... use doc1 and doc2 ... return null; });
Attempts:
2 left
💡 Hint
Reading the same document twice wastes reads and time.
✗ Incorrect
Option A avoids duplicate reads by calling get() once and reusing the document snapshot. Other options either do not reduce reads or add unnecessary complexity.
🔧 Debug
advanced3:00remaining
Why does this Firestore trigger not run?
A developer wrote this Firestore trigger:
But it never logs anything when a user document is deleted. What is the most likely reason?
exports.onDeleteUser = functions.firestore.document('users/{userId}').onDelete((snap, context) => { console.log('User deleted:', snap.id); return null;});But it never logs anything when a user document is deleted. What is the most likely reason?
Firebase
exports.onDeleteUser = functions.firestore.document('users/{userId}').onDelete((snap, context) => { console.log('User deleted:', snap.id); return null; });
Attempts:
2 left
💡 Hint
Check deployment and activation status of the function.
✗ Incorrect
If the function is not deployed or disabled, it will not run. snap.id is valid in onDelete triggers. The event name onDelete is correct. Returning null is allowed.
🧠 Conceptual
expert2:30remaining
Which Firestore trigger type can access both before and after data?
You want to write a Firestore trigger that runs when a document is updated and needs to compare the document's data before and after the update.
Which trigger type should you use?
Which trigger type should you use?
Attempts:
2 left
💡 Hint
Only some triggers provide both previous and new data snapshots.
✗ Incorrect
The onUpdate trigger provides a Change object with before and after snapshots to compare data. onWrite triggers also provide this but include create and delete events, so onUpdate is more specific. onCreate and onDelete only provide one snapshot.