Reputation: 35
I'm trying to retrieve the posts from all the users in Ionic(without having the specific user id). I can get all the posts from the first user in the database, but am having trouble with getting the posts from the other users.
This is a simplified version of my firebase structure:
"posts_meta" : {
"uid" : {
"post-id" : {
"title": "",
"content": ""
},
},
"uid" : {
"post-id" : {
"title": "",
"content": ""
},
"post-id" : {
"title": "",
"content": ""
},
}
And here I'm trying to loop through the users first and then loop through the posts for each user by using a nested foreach:
.factory('Timeline', function($q, Profile, Utils, Codes) {
var self = this;
self.getAllPosts = function() {
var qGet = $q.defer();
var ref = new Firebase(FBURL);
ref.child("posts_meta").once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
childSnapshot.forEach(function(grandchildSnapshot) {
qGet.resolve(grandchildSnapshot.val());
})
})
}, function (error) {
Codes.handleError(error);
qGet.reject(error);
});
return qGet.promise;
};
What am I doing wrong? Am I missing something?
Upvotes: 0
Views: 795
Reputation: 2663
You can't call multiple resolves for a single promise. The promise is being resolved in the first loop iteration and then the others are being ignored.
You can try save all granchilds to an array, but I believe firebase has its ways to solve this with a better performance.
var values = [];
snapshot.forEach(function(childSnapshot) {
childSnapshot.forEach(function(grandchildSnapshot) {
values.push(grandchildSnapshot);
})
})
promise.resolve(values)
Upvotes: 1