Firebase Cloud Functions中集合名称含空格部署触发报错求助
Looks like you ran into a common gotcha with Firebase Functions and Firestore paths—spaces in your collection/document path are breaking the deployment process, which explains why it works perfectly when you use paths without spaces.
Why This Happens
Firebase's deployment tool parses Firestore trigger paths as structured strings, and unescaped spaces get misinterpreted as unintended separators. It doesn't recognize the space as part of the actual collection name unless you explicitly tell it to treat it that way.
Fixes to Try
1. Escape Spaces in the Path
Add a backslash \ before each space in your path to escape it. This tells the deployment tool to treat the space as part of the collection name instead of a separator. Here's your modified code:
exports.testFunction = functions.firestore .document('/hospitals/Sidra\ Outpatient\ Hospital/users/noKW8jeJLLUeN1MTerHbCbGDzT72') .onWrite((event) => {});
2. Use Template Strings (Cleaner Alternative)
If you prefer a slightly cleaner syntax, you can use ES6 template strings—just remember to still escape the spaces:
exports.testFunction = functions.firestore .document(`/hospitals/Sidra\ Outpatient\ Hospital/users/noKW8jeJLLUeN1MTerHbCbGDzT72`) .onWrite((event) => {});
3. Rename the Collection (Long-Term Fix, If Possible)
If your business logic allows, renaming the collection to remove spaces (e.g., Sidra_Outpatient_Hospital or SidraOutpatientHospital) is a solid long-term solution. It eliminates the need for escaping entirely and aligns your paths with standard naming conventions.
Verify the Fix
After making one of these changes, re-run firebase deploy --only functions—your trigger should deploy successfully now, since the path will be parsed correctly.
内容的提问来源于stack exchange,提问作者zuest

