javascript - 带时间戳的 Firestore 查询

标签 javascript firebase google-cloud-firestore

如果它是文本字段,我可以使用 where 条件获取数据,但是当我尝试对时间戳字段和日期执行相同操作时,事情不起作用。

这是我的代码:

home.ts

firebase.firestore().collection("cities").where("timestamp", ">", "3/24/2020")
    .onSnapshot(function(querySnapshot) {
        var cities = [];
        querySnapshot.forEach(function(doc) {
            cities.push(doc.data().name);
        });
        console.log("Timestamp greather than 3/24/2020 -- ", cities.join(", "));
    });

firebase.firestore().collection("cities").where("state", "==", "CA") 一切正常,但不适用于 date .

火库截图:

enter image description here

注意:JLD 文档应在控制台中返回,因为其 timestamp 值大于 3/24/2020

编辑 1

按照@alex的指导,我现在正在这样做

let start = new Date('2020-03-25'); firebase.firestore().collection("cities").where("timestamp", ">", start)

但是当我使用>时它包含2020-03-25的数据,为什么?

最佳答案

I am trying to do the same with the timestamp field and date things are not working.

它不起作用,因为您传递给 .where("timestamp", ">", "3/24/2020") 的字符串自您的 timestamp 以来不正确数据库中的 属性包含一个 Date 对象,而不是一个 String。为了解决这个问题,不要将日期作为字符串(“3/24/2020”)传递,而是将其作为日期对象传递,一切都会正常工作。

let timestamp = new Date('2020-03-25');

然后使用:

firebase.firestore().collection("cities").where("timestamp", ">", timestamp);

关于javascript - 带时间戳的 Firestore 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60847774/

相关文章:

javascript - Aurelia:如何在绑定(bind)之前恢复到原始模型

firebase - react native 应用程序中 firebase 日志的 iOS 14.5 应用程序跟踪透明度

java - 从 FirestoreRecyclerView 添加多个项目到 recyclerview

javascript - clearRect 不清除 Canvas 中的任何内容

javascript - 错误:Object is not a function for returning an array to my callback function

javascript - Heroku 上的 Meteor 应用程序 sockjs 连接已关闭

javascript - 如何从代码中查找是否在本地运行的Firebase函数

java - list 合并失败: Attribute application@appComponentFactory cant solve this

firebase - 如何使用云功能通过签名下载url删除存储图像?

javascript - 使用无效数据调用函数 DocumentReference.set()。不支持的字段值 : a function (found in field dispatch)"