将数据推送到数组中,但在将时间戳转换为日期之后
Push data in array but after converting timestamp to date
正在从 Firestore 获取数据并将其推入数组。这工作正常,但在此过程中我正在更改时间戳格式,我希望数组中的 timestamp
值而不是原始值。如何替换原始值并推送?
到目前为止我的代码:
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);
});
});
正在从 Firestore 获取数据并将其推入数组。这工作正常,但在此过程中我正在更改时间戳格式,我希望数组中的 timestamp
值而不是原始值。如何替换原始值并推送?
到目前为止我的代码:
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);
});
});