javascript - orderByChild 和 startAt firebase 实时使用日期时间戳不返回数据

标签 javascript firebase firebase-realtime-database

- foo
|--- -LK7rNAPKZP7drHK1eb_
      |- timeStamp: 1534966072658
|--- -LKAPfu2lLl2B5zInIhX
      |- timeStamp: 1534965117552
|--- -LKJN97VxOhnE8XoNSQN
      |- timeStamp: 1534946072623

db.ref('foo').orderByChild('timeStamp').startAt(Date.now().toString()).limitToLast(7).once('value', data => {
    console.log(data.val());
}

结果为空。当我不放置 .startAt 时,我能够获取数据,并且它实际上是按时间戳排序的...但是,我需要能够设置 startAt 因为我正在进行无限滚动并且我需要能够指定最后的时间戳记录。我究竟做错了什么?我想拉出前7条记录

使用 Firebase 实时数据库

最佳答案

简而言之,不要在 Date.now() 上调用 toString(),只需将其保留为数字即可。

<小时/>

来自firebase docs :

4: Children with a numeric value come next, sorted in ascending order. If multiple children have the same numerical value for the specified child node, they are sorted by key.

5: Strings come after numbers and are sorted lexicographically in ascending order. If multiple children have the same value for the specified child node, they are ordered lexicographically by key.

如您所见,字符串值始终排在数字之后,因此通过在 startAt 上指定字符串,您可以保证结果中不会包含该字段的数字值条目。由于您的所有条目都是数字,这意味着您的结果为 null

关于javascript - orderByChild 和 startAt firebase 实时使用日期时间戳不返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51974375/

相关文章:

javascript - 将 Alertify 与 canDeactivate 结合使用

Javascript 无法在 Firefox 上执行,但可以在 Chrome 和 Safari 上执行

ios - iOS 的 cordova-firebase-plugin 配置

android - 如何在 Firebase 中检查写入任务是否成功

firebase-realtime-database - 如何计算firebase实时数据库中的键数

javascript - 如何从 JQuery 函数内将值推送到数组

javascript - 依赖/级联下拉菜单

ios - 删除键然后将其添加回来后,Firebase 子查询为零

android - 如何检查用户是否刚刚在 Firebase 中首次登录 Google

ios - Swift 3 Firebase - (setValue :) Cannot store object of type _SwiftValue at content