Firebase日期范围查询无效

时间:2016-08-25 13:49:29

标签: javascript firebase firebase-realtime-database

我正在尝试从Firebase执行两个单独的查询。第一个是一个通用查询,它获取保存的条目总数。这是正常的。

第二个应该是一个基于时间的范围查询,它确定今年保存的所有条目。

第一个查询正在运行。第二个是在控制台中显示“null”。它应该还显示“10”,因为所有查询都是在2016年创建的。

var ref = new Firebase('https://xxxxxxxx.firebaseio.com/entries');
    ref.once('value', function(snapshot) {
    $scope.allEntries = snapshot.numChildren();
        console.log('Total entries saved to date: ' + $scope.allEntries);
        // shows 10 entries, which is correct
    });

var ref2 = new Firebase('https://xxxxxx.firebaseio.com/');
ref2.child('entries')
    .startAt(14516244) // January 1st, 2016
    .endAt(1472131318) // current date (aug, 25th 2016)
    .once('value', function(snapshot) {
            console.log(snapshot.numChildren());
            // shows "null"
});

以下是第一个条目和最后一个条目的时间戳。

enter image description here

1 个答案:

答案 0 :(得分:5)

您在第二个查询中缺少orderByChild()

在查询开头添加:

ref2.child('entries')
    .orderByChild('createdAt')
    .startAt(14516244) // January 1st, 2016
    .endAt(1472131318) // current date (aug, 25th 2016)
    .once('value', function(snapshot) {
            console.log(snapshot.numChildren());
            // shows "null"
});

编辑:

第二个问题是您查询中的日期太小,因为它们在几秒钟内且您的 createdAt 以毫秒为单位。转换它。

ref2.child('entries')
    .orderByChild('createdAt')
    .startAt(14516244000) // January 1st, 2016
    .endAt(1472131318000) // current date (aug, 25th 2016)
    .once('value', function(snapshot) {
            console.log(snapshot.numChildren());
            // shows "null"
});
相关问题