【问题标题】:Firestore rules saying document get function is wrongFirestore 规则说文档获取功能是错误的
【发布时间】:2021-11-05 01:39:33
【问题描述】:

在我的新应用中,Firestore 中有一系列需要成本的项目。以下是我用来保护这些文件的规则:

rules_version = '2';

service cloud.firestore {
    match /databases/{database}/documents {
        match /projects/{project} {
            function isSignedIn() {
                return request.auth != null;
            }

            function isAdmin() {
                return isSignedIn() && get(/databases/(database)/documents/users/$(request.auth.uid)).data.isAdmin) == true);
            }

            allow read: if isSignedIn();
            allow create, update, delete: if isAdmin();
        }
    }
}

当我尝试部署此规则集时,我收到以下错误:

Error: Compilation errors in firestore.rules:
[E] 11:41 - Missing 'match' keyword before path.
[E] 11:51 - Forward slash '/' found where identifier or binding expected.    
[E] 11:52 - mismatched input '(' expecting {'{', '/', PATH_SEGMENT}
[E] 11:62 - Missing 'match' keyword before path.
[E] 11:62 - Unexpected '/documents'.
[E] 11:78 - Forward slash '/' found where identifier or binding expected.    
[E] 11:79 - mismatched input '$' expecting {'{', '/', PATH_SEGMENT}
[E] 11:98 - token recognition error at: '`'
[E] 23:1 - Unexpected '}'.

基本上它不喜欢get 行。但我是从 Firestore 文档 here 中得到的。有谁知道为什么这可能不起作用?

【问题讨论】:

  • 我认为你在 get 行中的 true 之后有多余的 ")"。确保管理员之后的那个,我在文档中知道它,但只需检查它。

标签: firebase google-cloud-firestore firebase-security


【解决方案1】:

你有一些小错误:

  1. 你有两个不需要)
  2. 您忘记了$ 之前的database

试试这个:

rules_version = '2';

service cloud.firestore {
    match /databases/{database}/documents {
        match /projects/{project} {
            function isSignedIn() {
                return request.auth != null;
            }

            function isAdmin() {
                return isSignedIn() && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.isAdmin == true;
            }

            allow read: if isSignedIn();
            allow create, update, delete: if isAdmin();
        }
    }
}

【讨论】:

  • 你碰巧知道 firebase .rules 的 linter 吗?
  • 不,但是firebase规则的控制台确实有助于发现问题
  • 这些规则没有报错,发布成功。
  • 谢谢。这也是 Firebase 支持回来的原因。
猜你喜欢
  • 2020-01-14
  • 2020-04-29
  • 2020-06-09
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
  • 2020-01-22
  • 2020-02-22
  • 2019-10-15
相关资源
最近更新 更多