Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
128
rated 0 times [  131] [ 3]  / answers: 1 / hits: 26716  / 9 Years ago, tue, november 24, 2015, 12:00:00

I am trying to query my database such that it retrieves an ordered list based on a child key. I do it as follows (see below), but nothing happens, meaning that it returns an object ordered exactly in the same way as it is stored in the Firebase database. What is going on?



self.getAllProfiles = function () {
var qProfile = $q.defer();
var ref = new Firebase(FBURL);
ref.child(users).orderByChild('last_update').on(value, function (snapshot) {
console.log(snapshot.val()) // HERE IS WHERE IT SHOULD BE ORDERED
qProfile.resolve(snapshot.val());
}, function (errorObject) {
qProfile.reject(errorObject);
});
return qProfile.promise;
};


To add, my users node looks as follows:



users
/$username
/last_update
/id
/data
/profile_image
/display_name


Here is a snapshot:



Tester: Object
github: Object
last_update: 1447732462170
userId: github:12345

More From » firebase

 Answers
96

When you call snapshot.val(), you are getting back a JSON object. The order of keys in a JSON object is determined by your browser and not by Firebase.


To get the children in order use the built-in forEach method of the snapshot:


self.getAllProfiles = function () {
var qProfile = $q.defer();
var ref = new Firebase(FBURL);
ref.child("users").orderByChild('last_update').on("value", function (snapshot) {
snapshot.forEach(function(child) {
console.log(child.val()) // NOW THE CHILDREN PRINT IN ORDER
});
qProfile.resolve(snapshot.val());
}, function (errorObject) {
qProfile.reject(errorObject);
});
return qProfile.promise;
};

You can leave the q.resolve() call where it is: snapshot.forEach() is not an asynchronous call.


[#64291] Saturday, November 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristineterrak

Total Points: 74
Total Questions: 109
Total Answers: 115

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
kristineterrak questions
Sun, Jul 25, 21, 00:00, 3 Years ago
Fri, Jul 31, 20, 00:00, 4 Years ago
Wed, Mar 4, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Sun, Apr 28, 19, 00:00, 5 Years ago
;