【问题标题】:How to write firebase rules with unique ID如何编写具有唯一 ID 的 Firebase 规则
【发布时间】:2016-12-05 06:15:41
【问题描述】:

这是我的问题:

WARN: FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "id_logement" at /images/-KNx2y3mAkJZ-Poa7R03 to your security rules for better performance 

我并不是真正的 firebase 和规则大师,所以这对我来说并没有什么意义……但我知道有一个问题!

这是我的数据结构:

这是我的规则!

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
    "accounts" : {
      ".read": true,
        ".write": true,
        ".indexOn": ["userId", "email", "lat",]
    },
     "geofire" : {
      ".read": true,
        ".write": true,
        ".indexOn": ["g"]
    },
      "logements" : {
      ".read": true,
        ".write": true,
        ".indexOn": ["id_account"]
    }



  }
}



我认为这是因为我不知道如何在规则中编写此唯一 ID!你能帮我吗?谢谢你

【问题讨论】:

    标签: firebase firebase-realtime-database firebase-security


    【解决方案1】:

    回答您的问题,您希望在规则中使用$id 表示法,它是表示分支键的通配符。

    {
      "rules": {
        ".read": "auth != null",
        ".write": "auth != null",
        ...
        "images": {
            "$imageId1": {
               ".indexOn": "id_logement"
            }
        }
      }
    }
    

    但是,请记住,您不应将 imagesUrl 存储在两级深度密钥中。使您的代码具有images/imageUniqueKey/imageData 结构而不是images/imageUniqueKey1/imageUniqueKey2/imageData。那么你的规则如下。

    "images": {
        ".indexOn": "id_logement"       
    }
    

    【讨论】:

    • 谢谢@adolfosrs !!对于你所有的建议:)
    猜你喜欢
    • 2018-04-27
    • 2016-09-29
    • 2018-08-27
    • 2018-11-07
    • 1970-01-01
    • 2017-07-11
    • 2018-05-24
    • 1970-01-01
    • 2019-04-30
    相关资源
    最近更新 更多