错误:未定义索引,添加。索引(&Q;)(&Q;) [英] "error": "Index not defined, add ".indexOn"
本文介绍了错误:未定义索引,添加。索引(&Q;)(&Q;)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在Firebase中创建了一个数据库,如下所示:
现在我进入REST客户端并发出以下查询:
https://movielens3.firebaseio.com/movieLens/users.json?orderBy="age"&startAt=25&print=pretty
它给我一个错误:
"error": "Index not defined, add ".indexOn": "age", for path "/movieLens/users", to the rules"
因此我进入规则部分并定义此规则:
{
"rules": {
"users" : {
".indexOn": ["age", "gender", "occupation", "zipCode"]
},
".read": true,
".write": true
}
}
但我仍然收到相同的错误。
推荐答案
您正在为/users
定义索引。在树根的正下方没有子节点users
,因此这些索引将为空。
您正在查询/movieLens/users
,因此应在该位置定义索引:
{
"rules": {
"movieLens": {
"users" : {
".indexOn": ["age", "gender", "occupation", "zipCode"]
},
".read": true,
".write": true
}
}
}
更新备注中的问题:
您将用户的年龄存储为字符串,因此不能将其作为数字进行过滤。解决方案是修复数据并将其存储为数字。但在此期间,此查询有点起作用:
https://movielens3.firebaseio.com/movieLens/users.json?orderBy="age"&equalTo="35"
在JavaScript中:
ref
.orderByChild('age')
.startAt('35')
.limitToFirst(3)
.once('value', function(s) {
console.log(JSON.stringify(s.val(), null, ' '));
}, function(error) {
if(error) console.error(error);
})
在某种程度上,它是按词汇排序的,而不是按数字排序的。为实际解决方案修复数据。
请注意,您还忽略了将数据存储为数组的Firebase最佳实践。这已经在REST API中给我带来了问题,这就是我删除print=pretty
的原因。我强烈建议您从头到尾阅读Firebase programming guide for JavaScript developers,并遵循其中的建议。现在需要几个小时,这将避免未来几个小时的问题。
这篇关于错误:未定义索引,添加。索引(&Q;)(&Q;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文