Reputation: 621
I have the following data structure
{
"advisors" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"description" : "",
"price" : 0,
"title" : ""
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"description" : "",
"price" : 0,
"title" : ""
},
"d8403e9b-fb74-4425-874f-2d125cd07a68" : {
"description" : "I know every corner in the arab countries.",
"price" : "50",
"title" : "Best guide in the middle east"
}
},
"advisors-countries" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"Angola" : "true"
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"Angola" : "true"
}
},
I want to make the following query: Give me all the prices of the advisors which have Angola in their list.
I tried the next code for retrieve only the child of Advisor-countries where Angola=true.
var advisors = (ref.orderByChild("Angole").equalTo(true));
$scope.users = $firebaseArray(advisors);
console.log("sync ", $scope.users);
But I'm keep getting null object in the advisors object.
Upvotes: 2
Views: 1176
Reputation: 32604
To do your query easily with AngularFire you'll need to rework your data structure.
"advisors-countries" : {
"0ea9bab6-415e-4900-8698-ac03a1ef4518" : {
"Angola" : "true",
"price": 0
},
"cc31c353-ca6a-440d-b188-af2016f72aef" : {
"Angola" : "true",
"price": 0
}
}
You should include the price in and any other relevant data in the advisors-countries
location. If you're worried about duplicating data, that's okay. You can use multi-path updates to keep your data consistent.
Then your query would work:
var query = ref.orderByChild("Angola").equalTo(true);
Now, why you're seeing null for $scope.users
.
When you log $scope.users
to the console, it's going to be null
because there's no data at first. Downloading the data is an asynchronous action, so it won't be available at first.
To debug this action, use $loaded()
.
$scope.users = $firebaseArray(advisors);
$scope.users.$loaded().then(function(data) {
console.log(data);
console.log(data === $scope.users);
});
In the first log, you'll get whatever data exists. In the second log, you'll see that the data
param is the same as the $scope.users
object. What this means is that you don't have use $loaded()
, because when the becomes available it will be used in your template.
Upvotes: 6