Vik
Vik

Reputation: 9299

firebase realtime DB querying date between 2 dates does not match anything

I am trying to find all the objects that are between two dates. the code look like below

getPendingOrders(dateFrom:number, dateTo:number){
    console.log('start of getPendingOrders with dateFrom:' + dateFrom + " dateTo:" + dateTo)
    return new Promise((resolve, reject) =>
      {
       this.db.list('orders',
                     ref =>  ref.orderByChild("order/_date").startAt(dateFrom).endAt(dateTo)
       ).snapshotChanges().subscribe(
        res => {
          console.log('response:' + JSON.stringify(res))
          resolve(res)
        },
        err => {
          console.log(err)
          reject(err)
        }
      )
      })
  }

The console is printing:

start of getPendingOrders with dateFrom:1538265600000 dateTo:1538352000000

My data is like below in db

"-LNc1K09FDXGW7PrS9wu" : {
    "order" : {
      "_date" : "1538269987967",
      "_deliveryType" : "Delivery",
      "_estDeliveryTime" : "12:NaN AM",
      "_isPaid" : false,
      "_location" : "djdjdj",
      "_orderNumber" : "VikKumar-87967",
    }
  },

but my code does not return any data. Not sure why? Just to add in this case if i remove .endAt then it works. so somehow upper bound does not work

Upvotes: 0

Views: 1469

Answers (2)

Renaud Tarnec
Renaud Tarnec

Reputation: 83153

The reason is that dateFrom and dateTo are numbers (See (dateFrom:number, dateTo:number)) and that you store _date as a string in your database.

Upvotes: 1

orimdominic
orimdominic

Reputation: 1135

Try changing this line:

ref => ref.orderByChild("order/_date").startAt(dateFrom).endAt(dateTo)

to this:

ref => ref.child("order").orderByChild("_date").startAt(dateFrom).endAt(dateTo)

Upvotes: 0

Related Questions