刚启动应用程序后,我没有登录,我尝试通过id读取文档:
static async getUserByUID(uid) {
let ref = firebase.firestore().collection("users");
let user = await ref.doc(uid).get()
return user
}
我得到一个错误:
Missing or insufficient permissions.
* http://127.0.0.1:19001/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&minify=false&hot=false:184506:21 in e
- node_modules/@firebase/firestore/dist/index.cjs.js:1:23672 in t.prototype.Vt
- node_modules/@firebase/firestore/dist/index.cjs.js:1:31796 in t.ee
- node_modules/@firebase/firestore/dist/index.cjs.js:1:148981 in <global>
- node_modules/@firebase/firestore/dist/index.cjs.js:1:48518 in e
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in setImmediate$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:146:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:194:17 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:458:30 in callImmediates
* [native code]:null in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:407:6 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:143:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:142:17 in __guard$argument_0
* [native code]:null in flushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
我的规则是:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
如果我打开所有权限,一切正常,有什么建议吗?
答案 0 :(得分:0)
您的规则要求用户登录(使用Firebase身份验证)才能访问数据。该错误消息表明用户尝试加载数据时未登录。
解决方案是在加载数据之前使用Firebase身份验证登录用户。例如:
await auth().signInAnonymously()
let ref = firebase.firestore().collection("users");
let user = await ref.doc(uid).get()
从您的评论中,您似乎对规则语言感到困惑。要允许对任何人进行读取,并且仅允许对经过身份验证的用户进行写入,您可以这样做:
allow read: if true;
allow write: if request.auth.uid != null;