Firebase get data by timestamp

空扰寡人 提交于 2020-06-17 00:05:55

问题


I need to filter data using to get specific data for the timestamp match.

For example I need the data where arrivalTime matches with exact date and time fields, timestamp field in database.

I am trying below but it returns no data.

  _arrivalTIme = moment(
            `${todaysDate.format('YYYY/MM/DD')} ${_arrivalTIme}:00`,
          ).toDate();

// _arrivalTIme: Thu May 28 2020 09:00:00 GMT-0600 (Mountain Daylight Time)

return firestore()
  .collection('mainCol')
  .doc(todayDate)
  .collection('subCol')
  .where('arrivalTime', '==', `${_arrivalTIme}`) ... 

my db record looks like this:

What am I doing wrong?


回答1:


You're comparing a date field in your database with a string from your code. That comparison will never be true.

You'll either need to get a Timestamp value that is exactly the same as the value in the database, or (more commonly) do a range check:

return firestore()
  .collection('mainCol')
  .doc(todayDate)
  .collection('subCol')
  .where('arrivalTime', '>=', new Date('2020-05-28 08:00:00')
  .where('arrivalTime', '<', new Date('2020-05-28 08:01:00')


来源:https://stackoverflow.com/questions/62071774/firebase-get-data-by-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!