orderByChild无法在Firebase中运行 [英] orderByChild not working in Firebase
问题描述
我正在尝试查询我的数据库,以便它根据子键检索有序列表。我这样做(见下文),但没有任何反应,这意味着它返回一个完全按照存储在Firebase数据库中的方式排序的对象。发生了什么?
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
这是一张快照:
Tester: Object
github: Object
last_update: 1447732462170
userId: "github:12345"
推荐答案
当你打电话给 snapshot.val()
时,你是获取JSON对象。 JSON对象中的键顺序由浏览器决定,而不是由Firebase决定。
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.
为了让孩子们顺序:
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;
};
您可以保留 q.resolve()
调用它的位置: snapshot.forEach()
不是异步调用。
You can leave the q.resolve()
call where it is: snapshot.forEach()
is not an asynchronous call.
这篇关于orderByChild无法在Firebase中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!