【发布时间】:2025-11-25 18:05:01
【问题描述】:
我想使用与时间戳相关的查询
注意:setSD和setED在Vue对象的数据中,调用firebase函数在方法中。
callFirebase: function (){
let startdate = new Date(this.setSD+'T00:00:00');
let enddate = new Date(this.setED+'T00:00:00');
console.log(startdate);
console.log(enddate);
db.collection("study").
where("time", ">=", firebase.firestore.Timestamp.fromDate(startdate)).
where("time", "<=", firebase.firestore.Timestamp.fromDate(enddate))
.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data().time.toDate());
});
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
}
}
错误图片:
callFirebase: function (){
let startdate = new Date(this.setSD+'T00:00:00');
let enddate = new Date(this.setED+'T00:00:00');
console.log(startdate);
console.log(enddate);
db.collection("study").
where("time", ">=", new Date(this.setSD+'T00:00:00')).
where("time", "<=", new Date(this.setED+'T00:00:00'))
.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data().time.toDate());
});
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
}
}
我也尝试过这种方式,但出现了同样的问题。
callFirebase: function (){
let startdate = new Date(this.setSD+'T00:00:00');
let enddate = new Date(this.setED+'T00:00:00');
console.log(startdate);
console.log(enddate);
db.collection("study").
where("time", ">=", new Date('2019-12-31T00:00:00')).
where("time", "<=", new Date('2020-01-01T00:00:00'))
.get().then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data().time.toDate());
});
})
.catch(function (error) {
console.log("Error getting documents: ", error);
});
}
}
但我最不能理解的是这个运行。 我的结论是我不能在 where 子句中使用变量。 但是再搜索了一下,好像不是这样的。有人可以帮我吗?
【问题讨论】:
-
能否请您展示一些 setSD 和 setED 值的示例?
标签: javascript firebase vue.js google-cloud-firestore