【问题标题】:Indexing 2nd child node in Firebase for REST Api access在 Firebase 中为 REST Api 访问索引第二个子节点
【发布时间】:2016-07-03 13:52:30
【问题描述】:

我正在使用 REST API 来查询 Firebase。

规则设置如下:

{
    "rules": {
        ".read": true,
        ".write": true,
        "user": {
            ".indexOn": "type"
        }
    }
}

使用 GET 方法查询以下 URL 时

https://exampleurl.firebaseio.com/user/9001.json

回应:

{
  "name": "Rohit",
  "person": {
    "-KLk3p3kUWg2j9p16kTw": {
      "mobile": "9002",
      "name": "Adarsh",
      "type": "D"
    },
    "-KLk4x2V_hfZwlsh6PMo": {
      "mobile": "9003",
      "name": "Manas",
      "type": "D"
    },
    "-KLk5-UPefdSMarC5VCQ": {
      "mobile": "9004",
      "name": "Sagar",
      "place": "thane",
      "type": "C"
    }
  }
}

在 GET 方法中使用过滤查询时出现错误

https://exampleurl.firebaseio.com/user/9001.json?orderBy="type"&startAt="D"

回应

{
  "error": "Index not defined, add \".indexOn\": \"type\", for path \"/user/9001\", to the rules"
}

我尝试使用以下规则。但是firebase不允许我保存。

{
    "rules": {
        ".read": true,
        ".write": true,
        "user/9001": {
            ".indexOn": "type"
        }
    }
}

Another question 谈到倾听变化。虽然我的问题是关于索引。

【问题讨论】:

标签: rest firebase firebase-realtime-database


【解决方案1】:

在搜索并花费了相当长的时间解决该问题后,能够找出在 Firebase 中索引第二个节点的解决方案。

Firebase 支持用于索引的通配符。

新规则,解决了我的问题:

{
    "rules": {
        ".read": true,
        ".write": true,
        "user": {
           "$person":{
            ".indexOn": "type"
            }
        }
    }
}

【讨论】:

  • 是 "$person" 的人 ID (9001) 还是你刚刚写了 "$person" ?
  • @AhmedHesham。您可以添加以$ 作为前缀的任何名称。这将充当通配符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 2017-10-17
  • 2011-07-22
  • 1970-01-01
  • 2019-01-07
  • 2015-04-06
  • 1970-01-01
相关资源
最近更新 更多