Casperkamal February 2016

Firebase timestamp filter

I'm new to firebase, actually I'm trying to load some data based on timestamp and retrieve between two timestamp using startat and endat. Data is pushed through Python.

The data is as shown in the sample screenshot

sample data

The data path 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

I'm pushing data through python setting the priority as timestamp. When I try to filter as shown here it returns null. But without startAt and endAt it shows all the values.

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

can anybody help me on what I'm doing wrong ?

Answers


Frank van Puffelen February 2016

You're trying to skip a level in your JSON tree:

var startTime = 1454924792;
var endTime = 1454924798;

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

I highly recommend that you don't depend on the implicit priority anymore and instead just filter using orderByChild()

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

You'll need to add an index to your Security Rules:

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

But the more explicit behavior is well worth the effort of adding this.

Post Status

Asked in February 2016
Viewed 1,665 times
Voted 7
Answered 1 times

Search




Leave an answer