我添加到安全和规则的 .indexOn 规则未被考虑在内
My .indexOn rule added to Security & Rules isn't taken into account
我正在尝试为我的 firebase 数据设置自定义索引,以便对某些字段进行排序。
这是我的数据:
获取https://testindexon.firebaseio.com/.json
{
"key1": {
"age": 80,
"name": "zabc"
},
"key2": {
"age": 90,
"name": "defg"
},
"key3": {
"age": 10,
"name": "hijk"
}
}
这是我的安全规则:
{
"rules": {
".read": true,
".write": true,
"testindexon": {
".indexOn": "age"
}
}
}
当我尝试按索引 "age" 属性 排序时,出现错误:
获取https://testindexon.firebaseio.com/.json?orderBy="age"
错误 400(错误请求)
{
"error": "Index not defined"
}
我可以通过如下重写我的安全规则来绕过 400 错误:
{
"rules": {
".read": true,
".write": true,
".indexOn": ["age", "name"]
}
}
但是当我尝试按其中一个属性排序时,没有发生任何特殊情况(结果未排序):
GET ....firebaseio.com/.json?orderBy="age"
{
"key1": {
"age": 80,
"name": "zabc"
},
"key2": {
"age": 90,
"name": "defg"
},
"key3": {
"age": 10,
"name": "hijk"
}
}
我是不是漏掉了什么?我将不胜感激任何帮助。
最佳,
状态变量
根据 Firebase documentation on retrieving data through its REST API:
THE REST API RETURNS UNSORTED RESULTS
JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
该文档中确实有一些样本和其他片段似乎暗示并非如此。但是上面的说法是对的。
请记住,orderBy
参数不仅仅用于 returning 排序结果。您还可以使用该方法来过滤结果,即
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&equalTo=90'
或者您可以订购 return 有限数量的结果:
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&limitToFirst=10'
所以在这些情况下,子节点在服务器上排序,然后过滤,但是return值will/may仍然包含未确定顺序的剩余子节点。
在我看来,您的 .indexOn 走错了路。您正在获取根目录,但将 .indexOn 放在子路径 testindexon/
下。因此,您的 URL 需要 https://testindexon.firebaseio.com/testindexon/.json
才能利用 orderBy。
我正在尝试为我的 firebase 数据设置自定义索引,以便对某些字段进行排序。
这是我的数据:
获取https://testindexon.firebaseio.com/.json
{
"key1": {
"age": 80,
"name": "zabc"
},
"key2": {
"age": 90,
"name": "defg"
},
"key3": {
"age": 10,
"name": "hijk"
}
}
这是我的安全规则:
{
"rules": {
".read": true,
".write": true,
"testindexon": {
".indexOn": "age"
}
}
}
当我尝试按索引 "age" 属性 排序时,出现错误:
获取https://testindexon.firebaseio.com/.json?orderBy="age"
错误 400(错误请求)
{
"error": "Index not defined"
}
我可以通过如下重写我的安全规则来绕过 400 错误:
{
"rules": {
".read": true,
".write": true,
".indexOn": ["age", "name"]
}
}
但是当我尝试按其中一个属性排序时,没有发生任何特殊情况(结果未排序):
GET ....firebaseio.com/.json?orderBy="age"
{
"key1": {
"age": 80,
"name": "zabc"
},
"key2": {
"age": 90,
"name": "defg"
},
"key3": {
"age": 10,
"name": "hijk"
}
}
我是不是漏掉了什么?我将不胜感激任何帮助。
最佳,
状态变量
根据 Firebase documentation on retrieving data through its REST API:
THE REST API RETURNS UNSORTED RESULTS
JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
该文档中确实有一些样本和其他片段似乎暗示并非如此。但是上面的说法是对的。
请记住,orderBy
参数不仅仅用于 returning 排序结果。您还可以使用该方法来过滤结果,即
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&equalTo=90'
或者您可以订购 return 有限数量的结果:
GET 'https://testindexon.firebaseio.com/.json?orderBy="age"&limitToFirst=10'
所以在这些情况下,子节点在服务器上排序,然后过滤,但是return值will/may仍然包含未确定顺序的剩余子节点。
在我看来,您的 .indexOn 走错了路。您正在获取根目录,但将 .indexOn 放在子路径 testindexon/
下。因此,您的 URL 需要 https://testindexon.firebaseio.com/testindexon/.json
才能利用 orderBy。