Reputation: 2267
I'm trying to retrieve all recipes favorited by a user using this code, but no data is retrived
recipesQuery = mDatabase.child("recipes").orderByChild("favoritedBy").equalTo(getId());
This is my Structure:
{
"-KNnrgEmF8qCpsWiIMW4" : {
"favoriteCount" : 4,
"favoritedBy" : {
"0ZtxMXZ8iDV4u0POxu8n6cIwCX33" : true
},
"name" : "Recipe 1",
"recycleCount" : 0,
},
"-KNvq6Ts8RYxc0-O3B2t" : {
"favoriteCount" : 2,
"favoritedBy" : {
"0ZtxMXZ8iDV4u0POxu8n6cIwCX33" : true
},
"name" : "Recipe 2",
"recycleCount" : 0,
}
}
If I remove the .equalTo()
method all data is retrieved, but thats not what I want.
What am I doing wrong?
Upvotes: 0
Views: 421
Reputation: 7720
There's currently no method to filter a query based on other objects than String, Boolean, or Double.
What I suggest is to restructure your database to something like this
{
"users": {
"userId1" : {
"name" : "User1",
"favorites": {
"-KNnrgEmF8qCpsWiIMW4" : true,
"-KNvq6Ts8RYxc0-O3B2t" : true
}
},
"userId2" : {
"name" : "User2",
"favorites": {
"-KNvq6Ts8RYxc0-O3B2t" : true
}
}
},
"recipes": {
"-KNnrgEmF8qCpsWiIMW4" : {
"favoriteCount" : 4,
"name" : "Recipe 1",
"recycleCount" : 0,
},
"-KNvq6Ts8RYxc0-O3B2t" : {
"favoriteCount" : 2,
"name" : "Recipe 2",
"recycleCount" : 0,
}
}
}
Then it's easier to get the recipes favorited by specific user.
mDatabase.child("users").child(userid).child("favorites")
Upvotes: 1