gpt4 book ai didi

javascript - 无法将变量输入到 Firebase 时间戳内的日期构造函数中?

转载 作者:行者123 更新时间:2023-12-02 22:11:05 25 4
gpt4 key购买 nike

我想使用与时间戳相关的查询

注意: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);
});
}
}

错误图片:

enter image description here

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 子句中使用变量。但稍微查了一下,似乎并不是这样。有人可以帮助我吗?

最佳答案

我自己解决了,这样做,

  callFirebase: function (){     
let startdate = new Date(this.setSD+'T00:00:00');
startdate.setHours(startdate.getHours()+9);
let enddate = new Date(this.setED+'T00:00:00');
enddate.setHours(enddate.getHours()+9);
console.log(startdate.toISOString());
console.log(enddate.toISOString().split('.',1)[0]);
db.collection("study").
where("time", ">=", new Date(startdate.toISOString().split('.',1)[0])).
where("time", "<=", new Date(enddate.toISOString().split('.',1)[0])).
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);
});
}
}

声明new Date()时,默认情况下在where子句中设置date.toISOString()(例如2020-01-21T00:00:00.000Z)

因此我使用 split() 将其转换为适合日期构造函数的字符串。

关于javascript - 无法将变量输入到 Firebase 时间戳内的日期构造函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59557657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com