Firebase存储安全规则调用Firestore.get报错求助
解决Firebase存储规则调用Firestore服务报错的问题
1. 开启模拟器跨服务访问支持
Firebase本地模拟器默认不会自动打通跨服务的规则调用,启动时得明确关联相关模拟器:
- 先单独启动Firestore模拟器:
firebase emulators:start --only firestore - 再启动存储模拟器,通过环境变量指定Firestore模拟器地址:
或者直接在FIRESTORE_EMULATOR_HOST=localhost:8080 firebase emulators:start --only storagefirebase.json里配置好模拟器关联,之后用firebase emulators:start一键启动所有服务,它们会自动互相识别:{ "emulators": { "firestore": { "port": 8080 }, "storage": { "port": 9199, "host": "localhost" }, "ui": { "enabled": true } } }
2. 核对Firestore路径与文档存在性
- 确认
data_users集合下存在报错里的UID对应的文档(报错里的UID是9eP2MrjhoQ2CR5w9GcW31hx6uZfF),firestore.get会因为目标文档不存在直接抛服务调用错误。 - 用
firestore.exists的时候,路径必须指向具体文档,不能是集合。你之前写的firestore.exists(/databases/(default)/documents/data_users)是检查集合路径,这肯定报错,正确写法是:firestore.exists(/databases/(default)/documents/data_users/$(request.auth.uid))。
3. 调整firebase-tools版本
你用的12.4.3版本可能存在跨服务规则调用的bug,试试升级到最新稳定版:
npm install -g firebase-tools@latest
如果升级后还是有问题,降级到11.x的稳定版本试试:
npm install -g firebase-tools@11.30.0
4. 检查Firestore自身的安全规则
要确保Firestore的规则允许存储服务访问目标文档,比如data_users集合的规则得加上:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /data_users/{user} { allow read: if request.auth.uid == user; } } }
不然就算存储规则调用了,Firestore那边也会拒绝访问,导致服务调用错误。
内容的提问来源于stack exchange,提问作者dacastror
相关产品推荐
相关产品推荐

