首页 > 解决方案 > Firestore 安全规则。允许阅读完整的集合(文档和子集合)

问题描述

我一直在尝试使用以下安全规则使子集合中的所有文档完全可读:

 match /books {
     allow write, update, delete: if false; // This collection cannot be modified
  
     match /sells {
         allow read: if true; // All documents and sub-collections of this collection are readable
     }
 }

但是,当我尝试阅读出售子集合的文档时,我收到 Firebase 权限错误。我究竟做错了什么?

标签: firebasegoogle-cloud-platformgoogle-cloud-firestorefirebase-security

解决方案


您需要匹配集合中的文档

 match /sells/{doc} {
     allow read: if true; // All documents in sells are readable
 }

如果您还想匹配sells文档的子集合,可以使用递归通配符,如下所示:

 match /sells/{doc=**} {
     allow read: if true; // All documents in and sub-collections of sells are readable
 }

推荐阅读