Firebase querying by date not working after deploy

﹥>﹥吖頭↗ 提交于 2020-07-08 03:24:18

问题


I'm working on a REST API with firebase and I'm querying some data by date. It works fine on the localhost but after deploy, it does not! I'm also using momentjs to help to deal with dates.

The date field of my document is a timestamp.

Here is how I'm doing my query:

const documents = await admin.firestore()
  .collection('orders')
  .where('date', '>', moment('2020-06-23'))
  .where('date', '<', moment('2020-06-24'))
  .get()

I have also done the same thing with Date() instead of momentjs

const documents = await admin.firestore()
  .collection('orders')
  .where('date', '>', new Date('Jun 23 2020'))
  .where('date', '<', new Date('Jun 24 2020'))
  .get()

Both of them works fine on localhost but after deploy the only way to fetch data from day 23 is passing day 24 and 25 on the query like below:

const documents = await admin.firestore()
  .collection('orders')
  .where('date', '>', moment('2020-06-24'))
  .where('date', '<', moment('2020-06-25'))
  .get()

Part of the data saved on my database:

I just want to fetch data by a specific date, what am I doing wrong?


回答1:


When you create a Date object in JavaScript without specifying a specific moment in time, it uses midnight of the provided date using the timezone of the machine running the code. Your computer's sense of midnight is going to be different than other computers in other timezones.

In order to do this query in a way that's predictable, you will have to come up with a specific moment in time, regardless of timezone, and use that in your queries.

Bear in mind that the timestamps you see in the Firebase console are formatted to use the local computer's sense of timezone. A timestamp in Firestore doesn't actually have any timezone - they are all measured in UTC.



来源:https://stackoverflow.com/questions/62605406/firebase-querying-by-date-not-working-after-deploy

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