angular - 将数据推送到数组中,但在将时间戳转换为日期之后
问题描述
从 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
:
解决方案
与其尝试更新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);
});
});