如果它是文本字段,我可以使用 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
.
火库截图:
注意: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/