Reputation: 29
I am trying a simple search in Firebase to return data from my Firebase data but nothing returns.
JSON STRUCTURE
{
"-JZQzhqTI6NUQMfgHwQc" : {
"Divisions" : {
"-JZLh2UYKi5IvphLtX4I" : true,
"-JZLh2Uc5HhffF1ki4H1" : true
},
"SportTypes" : {
"-JZLOh7KtM78I5wAL6Ua" : true
},
"name" : "winnipegjets",
"nickname" : "Jets",
"teamname" : "Winnipeg Jets"
}
}
My Firebase code to do a search is the following
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())
});
But once the code is execute it returns null. Also is there a way to search a specific field.
Upvotes: 0
Views: 1347
Reputation: 4539
If you are not setting priority. Then you need to orderByChild, inorder for the firebase to return result. In your case:
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())
});
Upvotes: 0
Reputation: 598728
It looks like you have a collection of teams, the first one is in a node named -JZQzhqTI6NUQMfgHwQc
(which was probably generated by calling push
). It has a property name
with a value of winnipegjets
and a property teamname
with a value of Winnipeg Jets
.
In a Firebase query you can filter nodes in two ways:
Your node is named -JZQzhqTI6NUQMfgHwQc
and it has a priority (that is not shown in the JSON you posted). Those are the only two values you can filter by.
If you're not using the priority yet, you might consider setting the name
or teamname
as the priority (look at the documentation for setPriority
) and filter on that.
It also looks like you are already trying to make the name
property unique. In that case, you could consider creating the structure as follows:
{
"winnipegjets" : {
"Divisions" : {
"-JZLh2UYKi5IvphLtX4I" : true,
"-JZLh2Uc5HhffF1ki4H1" : true
},
"SportTypes" : {
"-JZLOh7KtM78I5wAL6Ua" : true
},
"nickname" : "Jets",
"teamname" : "Winnipeg Jets"
}
}
Code for that would be something like:
var ref = new Firebase('https://sizzling-fire-6197.firebaseio.com/teams');
ref.child('winnipegjets').set(...);
Upvotes: 2