【问题标题】:Firebase timestamp filterFirebase 时间戳过滤器
【发布时间】:2016-05-18 08:04:16
【问题描述】:

我是 firebase 新手,实际上我正在尝试根据时间戳加载一些数据,并使用 startat 和 endat 在两个时间戳之间检索。数据通过 Python 推送。

数据如示例截图所示

sample data

数据路径popping-fire-7751.firebaseio.com/device488

activevolt: 396
avgPowerFactor: 0.95
avgThdi: 7.5
cubetemp: 37
datetime: "2016-02-08 15:16:32"
timestamp: 1454924792
totalPowerGen: 34

我正在通过 python 推送数据,将优先级设置为时间戳。 当我尝试按此处所示过滤时,它返回 null。但如果没有 startAt 和 endAt,它会显示所有值。

http://jsfiddle.net/casperkamal/EZUtP/64/

var startTime = 1454924792;
var endTime = 1454924798;

new Firebase("https://popping-fire-7751.firebaseio.com/")
    .startAt(startTime)
    .endAt(endTime)
    .once('value', show);

谁能帮我解决我做错了什么?

【问题讨论】:

  • 请不要将您的工作建立在过时的样本上。现在,您可以使用orderByChild('timestamp').startAt(startTime).endAt(endTime) 过滤任何孩子。不再需要使用优先级。
  • 谢谢@FrankvanPuffelen 你的回答很有帮助,效果很好。

标签: javascript firebase firebase-realtime-database


【解决方案1】:

您正在尝试跳过 JSON 树中的一个级别:

var startTime = 1454924792;
var endTime = 1454924798;

new Firebase("https://popping-fire-7751.firebaseio.com/")
    .child('device488')
    .startAt(startTime)
    .endAt(endTime)
    .once('value', show);

我强烈建议您不要再依赖隐式优先级,而只需使用 orderByChild() 进行过滤

new Firebase("https://popping-fire-7751.firebaseio.com/")
    .child('device488')
    .orderByChild('timestamp')
    .startAt(startTime)
    .endAt(endTime)
    .once('value', show);

您需要在安全规则中添加索引:

{
  "rules": {
    "device488": {
      ".indexOn": ["timestamp"]
    }
  }
}

但更明确的行为非常值得添加它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多