Firebase时间戳过滤器

时间:2016-02-08 15:36:44

标签: javascript firebase firebase-realtime-database

我是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);

任何人都能帮我解决我做错的事吗?

1 个答案:

答案 0 :(得分: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"]
    }
  }
}

但更明确的行为非常值得添加此功能。

相关问题