首页 > 解决方案 > 将数据推送到数组中,但在将时间戳转换为日期之后

问题描述

从 Firestore 获取数据并将其推送到数组中。这工作正常,但在这个过程中我正在更改时间戳格式,我希望这个值timestamp在我的数组中,而不是原来的。如何从 original 和 push 替换这个值?

到目前为止我的代码:

home.ts

firebase.firestore().collection("dailyreport").where("timestamp", ">=", start)
    .onSnapshot(querySnapshot => {
        var cities = [];
        querySnapshot.forEach( doc=> {

  const timeinmillsecs = doc.data().timestamp.seconds * 1000; 
  let a1 = new Date(timeinmillsecs); 
  let show_year_month_date =  a1.getFullYear()+'/'+(a1.getMonth()+1) +'/'+a1.getDate(); // 3.convert to imple date

  console.log(show_year_month_date); // ***NEED THIS VALUE IN dailyreports ARRAY 

         this.dailyreports.push(doc.data());
         console.log("Timestamp greather than 29march -- ", (doc.data()));
        });
    });

的截图console.log(doc.data())

在此处输入图像描述

编辑 1

在推送之前,我已经分配了这样的值 -

doc.data().timestamp = show_year_month_date;
this.dailyreports.push(doc.data());

但它也不起作用。

编辑 2

dailyreports和的截图doc

在此处输入图像描述

标签: angulartypescriptionic-frameworkgoogle-cloud-firestoreionic3

解决方案


与其尝试更新doc.data(),不如将值复制到一个新变量data中。更新时间戳,data然后将数据推送到this.dailyreports. 然后控制台日志data以查看具有更新时间戳的对象,该对象被推送到this.dailyreports.

firebase.firestore().collection("dailyreport").where("timestamp", ">=", start)
  .onSnapshot(querySnapshot => {
    var cities = [];
    querySnapshot.forEach(doc => {
      const data = doc.data();

      const timeinmillsecs = data.timestamp.seconds * 1000;
      let a1 = new Date(timeinmillsecs);
      let show_year_month_date = a1.getFullYear() + '/' + (a1.getMonth() + 1) + '/' + a1.getDate(); // 3.convert to imple date

      data.timestamp = show_year_month_date;
      console.log(show_year_month_date); // ***NEED THIS VALUE IN dailyreports ARRAY 
      


      this.dailyreports.push(data);
      console.log("Timestamp greather than 29march -- ", data);
    });
  });


推荐阅读