orderByChild 在 Firebase 中不起作用 [英] orderByChild not working in Firebase

查看:28
本文介绍了orderByChild 在 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.

要让孩子按顺序使用快照的内置forEach方法:

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;
};

您可以将 q.resolve() 调用保留在原处:snapshot.forEach() 不是异步调用.

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

这篇关于orderByChild 在 Firebase 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆