【问题标题】:Can't access collection inside document with rules in firestore无法使用 Firestore 中的规则访问文档内的集合
【发布时间】:2020-05-10 20:41:26
【问题描述】:

我正在尝试在我的应用程序中将一组文档放入这样的集合中:

const plans = await firebase.firestore()
                        .collection('users')
                        .doc(userId)
                        .collection('plans')
                        .get();

我的 Firestore 规则是这样设置的:

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()

它工作得很好。我已经坚持了一段时间。我错过了什么吗?

【问题讨论】:

    标签: node.js firebase google-cloud-firestore firebase-security


    【解决方案1】:

    问题是您只允许get 用于计划。 get 只允许个人文档访问。但是返回多个文档的查询需要list 访问权限(或read,这是getlist 的组合)。

    注意write也是createupdatedelete的组合,所以你可以这样更简洁:

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

    阅读更多关于granular operations in the documentation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 2018-07-04
      • 2022-01-05
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多