Complete the code to create a Firestore trigger that runs when a document is created.
exports.onUserCreate = functions.firestore.document('users/{userId}').[1]((snap, context) => { const newValue = snap.data(); console.log('User created:', newValue); });
The onCreate trigger runs only when a new document is created in Firestore.
Complete the code to access the document ID inside a Firestore trigger function.
exports.logDocId = functions.firestore.document('posts/{postId}').onWrite((change, context) => { const docId = context.params.[1]; console.log('Document ID:', docId); });
The context.params object contains the wildcard values from the document path. Here, postId matches the wildcard in the path.
Fix the error in the Firestore trigger to correctly get the new data after an update.
exports.onPostUpdate = functions.firestore.document('posts/{postId}').onUpdate((change, context) => { const newData = change.[1].data(); console.log('Updated data:', newData); });
In an onUpdate trigger, change.after contains the new document snapshot after the update.
Fill both blanks to create a Firestore trigger that runs on document deletion and logs the deleted data.
exports.onDeletePost = functions.firestore.document('posts/{postId}').[1]((snap, [2]) => { const deletedData = snap.data(); console.log('Deleted post:', deletedData); });
The onDelete trigger runs when a document is deleted. The second parameter is usually called context to access event info.
Fill all three blanks to create a Firestore trigger that runs on any write and logs old and new data.
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); });
onWrite triggers on create, update, or delete. change.before is the old data snapshot. The second parameter is the event context.