firebase / firebase/firebase-tools
Firebase Cloud Functions: Document deletion causes unexpected onDocumentCreated function executions
- Dominant language
- TypeScript
- Stars
- 4.5k
- Forks
- 1.3k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 84
Description
### [REQUIRED] Environment info
**firebase-tools:** 13.22.1 / 14.10.1
**Platform:** MacBook Pro M2
### [REQUIRED] Test case
**Function 1**
```
// Attempting to delete a document that may not exist
export const deleteUserData = onDocumentCreated(
{
document: "deleteUser/{userId}",
},
async (event) => {
const userId = event.params.userId;
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')
}
);
```
### [REQUIRED] Steps to reproduce
1. Manually create a document at path deleteUser called test_user123 (final path deleteUser/test_user123).
2. Make sure test_user123 is not present in userData collection and is random .
### [REQUIRED] Expected behavior
When a document is created at deleteUser/{userId}:
1. only trigger the deleteUserData function since that is the only function listening to the deleteUser/{userId} path via onDocumentCreated.
### [REQUIRED] Actual behavior
1. Unexpected cross-triggering: When a document is created in deleteUser/{userId}, my deleteUserData function correctly triggers and attempts to delete a document from the "userData" collection.
2. The main problem: If the document being deleted doesn't exist, the checker function (which should only trigger on userData/{userId} document creation) gets triggered unexpectedly.
```
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
```
Contributor guide
Assessment
This issue has not been assessed yet.