[英]Firestore query with timestamp
如果它是一个文本字段,我可以使用 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")
一切正常firebase.firestore().collection("cities").where("state", "==", "CA")
但不适用于date
。
消防栓截图:
注意: JLD
文档应该在控制台中返回,因为它的timestamp
值大于3/24/2020
在@alex 的指导下,我现在正在这样做
let start = new Date('2020-03-25');
firebase.firestore().collection("cities").where("timestamp", ">", start)
但是当我使用>
时它包括2020-03-25
的数据,为什么?
我正在尝试对时间戳字段执行相同的操作,但日期无法正常工作。
它不起作用,因为您正在向.where("timestamp", ">", "3/24/2020")
一个不正确的字符串,因为您在数据库中的timestamp
属性包含一个Date
对象而不是一个字符串。 为了解决这个问题,不要将日期作为字符串(“3/24/2020”)作为日期对象传递,一切都会正常工作。
let timestamp = new Date('2020-03-25');
然后使用:
firebase.firestore().collection("cities").where("timestamp", ">", timestamp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.