部署后按日期查询Firebase无效 [英] Firebase querying by date not working after deploy

查看:101
本文介绍了部署后按日期查询Firebase无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase开发REST API,并按日期查询一些数据.它可以在localhost上正常运行,但是在部署后却无法正常运行!我也在使用momentjs来帮助处理日期.

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.

这是我的查询方式:

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

我也用Date()代替了momentjs做过同样的事情

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

它们两者都可以在localhost上正常工作,但是在部署后,从第23天获取数据的唯一方法是在查询中传递第24和25天,如下所示:

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?

推荐答案

在JavaScript中创建Date对象而未指定特定时间的情况下,它将使用提供代码的午夜时间(使用运行代码的计算机的时区).您的计算机的午夜感觉将不同于其他时区的其他计算机.

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.

请记住,您在Firebase控制台中看到的时间戳被格式化为使用本地计算机的时区. Firestore中的时间戳实际上没有任何时区-它们都是以UTC度量的.

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.

这篇关于部署后按日期查询Firebase无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆