无法使用 Firestore 中的规则访问文档内的集合



我正在尝试在我的应用程序中获取集合中的一组文档,如下所示:

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也是createupdatedelete的组合,所以你可以像这样更简洁:

match /plans/{document=**}  {
allow read, write: if request.auth.uid == userId;
}

在文档中阅读有关粒度操作的更多信息。

最新更新