Liv*_*i17 1 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"
});
Run Code Online (Sandbox Code Playgroud)
以下是第一个条目和最后一个条目的时间戳.
您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"
});
Run Code Online (Sandbox Code Playgroud)
编辑:
第二个问题是查询中的日期太小,因为它们在几秒钟内,而您的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"
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
534 次 |
| 最近记录: |