【问题标题】:Can't input variable into Date constructor inside timestamp in firebase?无法将变量输入到 Firebase 中时间戳内的 Date 构造函数中?
【发布时间】: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


【解决方案1】:

我自己就是这样解决的,

  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() 时,date.toISOString() 在 where 子句中默认设置。 (such as 2020-01-21T00:00:00.000Z)

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

【讨论】: