我正在尝试在我的应用程序中获取集合中的一组文档,如下所示:
const plans = await firebase.firestore()
.collection('users')
.doc(userId)
.collection('plans')
.get();
我的消防店规则是这样设置的:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow create: if request.auth.uid != null;
allow get: if request.auth.uid == userId;
match /plans/{document=**} {
allow create, get, update, delete: if request.auth.uid == userId;
}
}
}
}
每次尝试查询计划集合中的所有文档时,都会收到此错误
FirebaseError: Missing or insufficient permissions
但是如果我尝试只查询用户文档
firebase.firestore().collection('users').doc(userId).get()
它工作得很好。我已经坚持了一段时间了。我错过了什么吗?
问题是你只允许get
计划。get
仅允许单个文档访问。 但是返回多个文档的查询需要list
访问权限(或read
,这既是get
又是list
组合(。
请注意,write
也是create
、update
和delete
的组合,所以你可以像这样更简洁:
match /plans/{document=**} {
allow read, write: if request.auth.uid == userId;
}
在文档中阅读有关粒度操作的更多信息。