use*_*442 0 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(", "));
});
Run Code Online (Sandbox Code Playgroud)
一切正常,firebase.firestore().collection("cities").where("state", "==", "CA")但不适用于date.
消防栓截图:
注意:JLDdoc 应该在控制台返回,因为它的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');
Run Code Online (Sandbox Code Playgroud)
然后使用:
firebase.firestore().collection("cities").where("timestamp", ">", timestamp);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2500 次 |
| 最近记录: |