Firebase查询未返回数据库中的任何结果
问题描述:
我正在尝试在Firebase中进行简单搜索以从我的Firebase数据中返回数据,但没有任何结果。Firebase查询未返回数据库中的任何结果
JSON结构
{
"-JZQzhqTI6NUQMfgHwQc" : {
"Divisions" : {
"-JZLh2UYKi5IvphLtX4I" : true,
"-JZLh2Uc5HhffF1ki4H1" : true
},
"SportTypes" : {
"-JZLOh7KtM78I5wAL6Ua" : true
},
"name" : "winnipegjets",
"nickname" : "Jets",
"teamname" : "Winnipeg Jets"
}
}
我的火力地堡代码做搜索是以下
new Firebase("https://sizzling-fire-6197.firebaseio.com/teams")
.startAt('Winnipeg Jets')
.endAt('Winnipeg Jets')
.once('value', function(snap) {
console.log('matching team is', snap.val())
});
但一旦代码执行它返回null。还有一种搜索特定领域的方法。
答
看起来您拥有一组团队,第一个团队位于名为-JZQzhqTI6NUQMfgHwQc
的节点(可能是通过调用push
生成的)。它有一个name
的财产winnipegjets
和属性teamname
与价值Winnipeg Jets
。
在Firebase query you can filter nodes in two ways:
- 他们的名字
- 按优先
你的节点被命名为-JZQzhqTI6NUQMfgHwQc
,它有一个优先级(未在您发布的JSON所示) 。这些是您可以过滤的唯一两个值。
如果您尚未使用优先级,则可以考虑将name
或teamname
作为优先级(请参阅setPriority
的文档)并对其进行过滤。
它也看起来像你已经试图使name
财产独特。在这种情况下,你可以考虑创建结构如下:
{
"winnipegjets" : {
"Divisions" : {
"-JZLh2UYKi5IvphLtX4I" : true,
"-JZLh2Uc5HhffF1ki4H1" : true
},
"SportTypes" : {
"-JZLOh7KtM78I5wAL6Ua" : true
},
"nickname" : "Jets",
"teamname" : "Winnipeg Jets"
}
}
代码,这将是这样的:
var ref = new Firebase('https://sizzling-fire-6197.firebaseio.com/teams');
ref.child('winnipegjets').set(...);
答
如果不设置优先级。然后你需要orderByChild,为firebase命令返回结果。你的情况:
new Firebase("https://sizzling-fire-6197.firebaseio.com/teams")
.orderByChild('teamname')
.startAt('Winnipeg Jets')
.endAt('Winnipeg Jets')
.once('value', function(snap) {
console.log('matching team is', snap.val())
});
相关的http://stackoverflow.com/questions/25272919/how-to-query-firebase-for-property-with-specific-value-inside-all-children,HTTP: //stackoverflow.com/questions/23812624/firebase-where-like-search,http://stackoverflow.com/questions/11587775/database-style-queries-with-firebase,http://stackoverflow.com/questions/ 14963776/get-users-by-name-property-using-firebase并且可能还有其他一些。 – 2014-10-19 19:48:41