0
0
Firebasecloud~20 mins

Firestore trigger functions in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Firestore Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this Firestore trigger function log?
Consider this Firestore trigger function that runs when a document is created in the 'users' collection:
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;
});
AError: snap.data is not a function
BNew user email: undefined
CNew user email: alice@example.com
DNo output, function does not run on create
Attempts:
2 left
💡 Hint
Remember that snap.data() returns the document data when a document is created.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Firestore trigger
Which option contains the correct syntax for a Firestore onUpdate trigger on the 'orders' collection?
Afunctions.firestore.document('orders/{orderId}').onUpdate((change, context) => { return null; })
Bfunctions.firestore.document('orders/{orderId}').onUpdate((change, context) => { return null; }
Cfunctions.firestore.document('orders/{orderId}').onUpdate(change, context) => { return null; }
Dfunctions.firestore.document('orders/{orderId}').onUpdate((change, context) => return null; )
Attempts:
2 left
💡 Hint
Check the arrow function syntax and parentheses.
optimization
advanced
3:00remaining
Optimize this Firestore trigger to reduce reads
This Firestore onWrite trigger reads the same document twice:
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;
});
AUse a single get() call and reuse the result: const doc = await ...get(); then use doc twice.
BReplace get() with onSnapshot() to listen for changes.
CCall get() twice but cache the second call result in a variable.
DUse two separate get() calls but add a delay between them.
Attempts:
2 left
💡 Hint
Reading the same document twice wastes reads and time.
🔧 Debug
advanced
3:00remaining
Why does this Firestore trigger not run?
A developer wrote this Firestore trigger:
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;
});
Asnap.id is undefined in onDelete triggers.
BThe function is not deployed or not enabled in Firebase console.
ConDelete triggers require a different event name.
DThe function must return a promise, returning null causes it to not run.
Attempts:
2 left
💡 Hint
Check deployment and activation status of the function.
🧠 Conceptual
expert
2: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?
AonWrite trigger
BonDelete trigger
ConCreate trigger
DonUpdate trigger
Attempts:
2 left
💡 Hint
Only some triggers provide both previous and new data snapshots.