首页 > 解决方案 > get() 不起作用,错误不遵循文档

问题描述

添加 Firestore 规则以确保用户只能访问其公司的数据。company 是用户集合中他们记录中的一个字段。馆藏名称与公司名称相同。

根据 Firestore 文档,这就是我认为它应该工作的方式。我确实尝试了一些对我来说更有意义的事情,比如添加括号,删除错误消息中说错误的斜线,但得到了相同的结果。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
    allow read, write: if false;
    }

    match /{collectionName}/{document=**} {
      allow read: if get(databases/$(database)/documents/users/$(request.auth.uid)).data.company == collectionName;
      }

  }
}
      }

允许读取 if get(..line 是第 22 行。这是我尝试发布规则集时收到的错误消息..

 Line 22: Unexpected '$'.; Line 22: Missing 'match' keyword before path.; 
Line 22: Forward slash '/' found where identifier or binding expected.; 
Line 22: mismatched input '$' expecting {'{', '/', PATH_SEGMENT}; 
Line 22: Missing 'match' keyword before path.; 
Line 22: Forward slash '/' found where identifier or binding expected.; 
Line 22: mismatched input '$' expecting {'{', '/', PATH_SEGMENT}

对我来说,这些错误都没有任何意义,因为 match 在那里,以及我刚刚从文档中复制的所有其他内容......作为参考,这里是 Firebase 文档中的 get() 示例:

allow delete: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true

我错过了什么?

标签: google-cloud-firestorefirebase-security

解决方案


推荐阅读