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

标签 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(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/

相关文章:

android - 火力地堡数据库 : How to set default value (Timestamp) to field

javascript - 从 Vue.js 中的文档元素获取值

javascript - 获取 0 的 documentElement 返回什么

javascript - 尝试在重新加载时存储单选按钮选择

python - 我如何在 Firebase 云存储上创建存储桶

android - 区分 firstore 中的首次查询快照和更改监听器

javascript - 更新 v-for 中使用的数组内的对象时,Vue js react 性问题

javascript - 如何将元素从已安装的元素传递到 HTML 模板

javascript - IndexedDB 的锁定模型?

javascript - 如何获取地标上点击点的经度、纬度和海拔高度?