【问题标题】:Spaces in collection name for Firebase Firestore security rulesFirebase Firestore 安全规则的集合名称中的空格
【发布时间】:2018-01-10 17:07:31
【问题描述】:

例如,我有一个集合“销售助理”。我正在尝试编写安全规则:

allow read: if exists(/databases/$(database)/documents/sales associates/$(request.auth.uid));

但我收到一个错误,因为“销售人员”中有一个空格。你如何在集合名称中写一个空格?围绕销售人员写报价是行不通的。

【问题讨论】:

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


    【解决方案1】:

    在整个事情上加上引号:

    allow read: if exists("/databases/$(database)/documents/sales associates/$(request.auth.uid)");
    

    【讨论】:

    • 模拟器出现“错误运行模拟”错误。显然在集合名称中有|; 甚至- 之类的字符返回Simulated read denied 且没有错误。请修复或记录!
    【解决方案2】:

    我为此苦苦挣扎,最后写信给 Google 寻求支持。这是我回来的:

    对于将 get() 方法与名称中带有空格的集合和/或文档一起使用的用例,您可以在自定义函数中抽象 get() 方法,以便它处理字符串变量而不是字符串文字.下面是一个夸张的例子来说明如何做到这一点:

    match /collection%20with%20space/{doc}/subcollection%20with%20space/{subDoc} {
      allow read, write: 
          if getDoc("collection with space", doc).data['field with space'] == "foo";
    }
    
    function getDoc(collectionString, documentString) {
      return get(/databases/$(database)/documents/$(collectionString)/$(documentString));    
    }
    
    

    虽然规则模拟器可能会遇到问题,但它应该可以与实时 SDK 一起正常工作。

    确实,模拟器抛出了错误,但运行应用程序确实有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-01
      • 2018-09-26
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 2021-01-10
      • 2021-04-09
      • 1970-01-01
      相关资源
      最近更新 更多