最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore query with timestamp - Stack Overflow

programmeradmin3浏览0评论

I am able to fetch the data using where condition if it's a text field but when I am trying to do the same with timestamp field and date things are not working.

Here is my code:

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(", "));
    });

Everything works fine with firebase.firestore().collection("cities").where("state", "==", "CA") but not working with date.

Screenshot of fire storage:

Note: JLD doc should be returned in console because its timestamp value is greater than 3/24/2020

Edit 1

As guided by @alex now I am doing like this

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

but it is including the data of 2020-03-25 when I am using >, why ?

I am able to fetch the data using where condition if it's a text field but when I am trying to do the same with timestamp field and date things are not working.

Here is my code:

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(", "));
    });

Everything works fine with firebase.firestore().collection("cities").where("state", "==", "CA") but not working with date.

Screenshot of fire storage:

Note: JLD doc should be returned in console because its timestamp value is greater than 3/24/2020

Edit 1

As guided by @alex now I am doing like this

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

but it is including the data of 2020-03-25 when I am using >, why ?

Share Improve this question edited Mar 29, 2020 at 9:40 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Mar 25, 2020 at 11:21 user2828442user2828442 2,5157 gold badges66 silver badges120 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

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

It's not working because you are passing to the .where("timestamp", ">", "3/24/2020") a String which is not correct since your timestamp property in the database holds a Date object and not a String. To solve this, instead of passing the date as a String ("3/24/2020") pass it as a Date object and everything will work fine.

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

And then use:

firebase.firestore().collection("cities").where("timestamp", ">", timestamp);
发布评论

评论列表(0)

  1. 暂无评论