Pablo DelaNoche
Pablo DelaNoche

Reputation: 677

Firebase simple query date range ... doesn't work

I have an array with some keys :

['-KSH1rJ8BgbOWKdwdn0J', '-KSH0B8MfPCB2_zI8H3Q', ...];

And for each key, I would like to know if "ts_exp" which is the expiration timestamp is between yesterday and tomorrow which are both timestamps.

 var yesterday = moment().subtract(1, 'day').format("x");
 var tomorrow = moment().add(1, 'day').format("x");

 foreach(... as key){
        var rootRef = firebase.database().ref('demandes').child(key)
        .orderByChild('ts_exp')
        .startAt(yesterday)
        .endAt(tomorrow);

        rootRef.once("value", function(messageSnapshot) {
          console.log(messageSnapshot.val()); // always return null
        });
    }

What's wrong with this query? This is this always null (But I have a "demande" which have an "ts_exp" between this 2 values). I did .indexOn "ts_exp" in my firebase rules also.
Thank you
This is the data structure :

 -demandes
    ----- -KSH1rJ8BgbOWKdwdn0J
    ---------- ts_exp : 14123123123,
    ---------- title : "pipo1",
    ---------- desc : "desc"
    ----- -KSH0B8MfPCB2_zI8H3Q
    ---------- ts_exp : 14123176576,
    ---------- title : "pipo2",
    ---------- desc : "desc2"

Upvotes: 0

Views: 137

Answers (1)

Michael Lehenbauer
Michael Lehenbauer

Reputation: 16319

Looks like your ts_exp values are numbers (14123123123) but your startAt / endAt are strings, and Firebase Database sorts all numbers before all strings, so your query is searching a range with no data.

Untested, but try:

var yesterday = moment().subtract(1, 'day').valueOf();
var tomorrow = moment().add(1, 'day').valueOf();

Upvotes: 1

Related Questions