Javascript Can';是否将变量输入firebase中时间戳内的日期构造函数?
我想使用与时间戳相关的查询 注意:setSD和setED在Vue对象的数据中,调用firebase函数在方法中Javascript Can';是否将变量输入firebase中时间戳内的日期构造函数?,javascript,firebase,vue.js,google-cloud-firestore,Javascript,Firebase,Vue.js,Google Cloud Firestore,我想使用与时间戳相关的查询 注意: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(e
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(){
让startdate=新日期(this.setSD+'T00:00:00');
让enddate=新日期(this.setED+'T00:00:00');
控制台日志(起始日期);
console.log(enddate);
db.收集(“研究”)。
其中(“time”,“>=”,firebase.firestore.Timestamp.fromDate(startdate))。
其中(“time”,doc.data().time.toDate());
});
})
.catch(函数(错误){
log(“获取文档时出错:”,错误);
});
}
}
错误图像:
callFirebase:function(){
让startdate=新日期(this.setSD+'T00:00:00');
让enddate=新日期(this.setED+'T00:00:00');
控制台日志(起始日期);
console.log(enddate);
db.收集(“研究”)。
其中(“time”,“>=”,新日期(this.setSD+'T00:00:00'))。
其中(“time”,doc.data().time.toDate());
});
})
.catch(函数(错误){
log(“获取文档时出错:”,错误);
});
}
}
我试着这样做,但同样的问题发生了
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);
});
}
}
callFirebase:function(){
让startdate=新日期(this.setSD+'T00:00:00');
让enddate=新日期(this.setED+'T00:00:00');
控制台日志(起始日期);
console.log(enddate);
db.收集(“研究”)。
其中(“时间”,“>=”,新日期('2019-12-31T00:00:00'))。
其中(“time”,doc.data().time.toDate());
});
})
.catch(函数(错误){
log(“获取文档时出错:”,错误);
});
}
}
但我最不能理解的是,这是一个运行。
我的结论是我不能在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);
});
}
}
callFirebase:function(){
让startdate=新日期(this.setSD+'T00:00:00');
startdate.setHours(startdate.getHours()+9);
让enddate=新日期(this.setED+'T00:00:00');
enddate.setHours(enddate.getHours()+9);
log(startdate.toISOString());
log(enddate.toISOString().split('.',1)[0]);
db.收集(“研究”)。
其中(“time”,“>=”,新日期(startdate.toISOString().split(“.”,1)[0])。
其中(“time”,doc.data().time.toDate());
});
})
.catch(函数(错误){
log(“获取文档时出错:”,错误);
});
}
}
声明新日期()
时,默认情况下在where子句中设置日期.toISOString()
。
(例如2020-01-21T00:00:00.000Z)
因此,我使用split()将其转换为适合日期构造函数的字符串。您能展示一些setSD和setED值的示例吗?
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);
});
}
}