0
0
Firebasecloud~10 mins

Firestore trigger functions in Firebase - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Firestore trigger that runs when a document is created.

Firebase
exports.onUserCreate = functions.firestore.document('users/{userId}').[1]((snap, context) => {
  const newValue = snap.data();
  console.log('User created:', newValue);
});
Drag options to blanks, or click blank then click option'
AonDelete
BonUpdate
ConWrite
DonCreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using onUpdate instead of onCreate will trigger only on document changes, not creation.
Using onDelete triggers when a document is deleted, not created.
2fill in blank
medium

Complete the code to access the document ID inside a Firestore trigger function.

Firebase
exports.logDocId = functions.firestore.document('posts/{postId}').onWrite((change, context) => {
  const docId = context.params.[1];
  console.log('Document ID:', docId);
});
Drag options to blanks, or click blank then click option'
AdocId
BdocumentId
CpostId
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name that does not match the wildcard in the path.
Trying to get the ID from the change object instead of context.params.
3fill in blank
hard

Fix the error in the Firestore trigger to correctly get the new data after an update.

Firebase
exports.onPostUpdate = functions.firestore.document('posts/{postId}').onUpdate((change, context) => {
  const newData = change.[1].data();
  console.log('Updated data:', newData);
});
Drag options to blanks, or click blank then click option'
Abefore
Bafter
Cprevious
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using change.before.data() returns the old data before the update.
Using change.new.data() is not a valid property.
4fill in blank
hard

Fill both blanks to create a Firestore trigger that runs on document deletion and logs the deleted data.

Firebase
exports.onDeletePost = functions.firestore.document('posts/{postId}').[1]((snap, [2]) => {
  const deletedData = snap.data();
  console.log('Deleted post:', deletedData);
});
Drag options to blanks, or click blank then click option'
AonDelete
Bcontext
Cevent
DonRemove
Attempts:
3 left
💡 Hint
Common Mistakes
Using onRemove instead of onDelete is incorrect for Firestore triggers.
Naming the second parameter something other than context or event is allowed but context is standard.
5fill in blank
hard

Fill all three blanks to create a Firestore trigger that runs on any write and logs old and new data.

Firebase
exports.onAnyWrite = functions.firestore.document('items/{itemId}').[1]((change, [2]) => {
  const oldData = change.[3].data();
  const newData = change.after.data();
  console.log('Old data:', oldData);
  console.log('New data:', newData);
});
Drag options to blanks, or click blank then click option'
AonWrite
Bcontext
Cbefore
DonUpdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using onUpdate instead of onWrite limits the trigger to updates only.
Trying to get old data from change.after instead of change.before.