I have two Firebase Cloud Functions with onDocumentCreated triggers. The issue is that when my delete function tries to delete a document that doesn't exist, it somehow triggers the checker function, which should only fire when documents are created in the userData collection. This cross-triggering behavior is causing unexpected side effects in my application.
These functions are run in Firebase emulator not production.
Function 1
// Attempting to delete a document that may not exist
export const deleteUserData = onDocumentCreated(
{
document: "deleteUser/{userId}",
},
async (event) => {
const db = firebase_admin.firestore();
const tokenDocRef = db.collection("userData").doc(userId);
tokenDocRef.delete();
}
);
Function 2
// This should only trigger when documents are created in userData collection
export const checker = onDocumentCreated(
{
document: "userData/{userId}",
},
async (event) => {
console.log('triggered')
}
);
Issues I'm Facing
Execution logs showing the issue:
Beginning execution of "asia-south1-deleteUserData"
Finished "asia-south1-deleteUserData" in 5.352708ms
Beginning execution of "asia-south1-checker" // This shouldn't happen!
Finished "asia-south1-checker" in 5.352708ms
Environment
An obvious way to fix this is to fetch the document , check if it exist and return if it doesn't exist . But I am trying to understand why is this happening in the first place.
Confirmed by the Firebase team, it was a bug. My submitted report has been accepted.