Issue
Fetching data from Firestore and pushing it into an array. This is working fine but in this process I am changing timestamp format and I want this value of timestamp
in my array and not the original one. How do I replace this value from original and push ?
My code so far:
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()));
});
});
The screenshot of console.log(doc.data())
Edit 1
Before pushing, I have assigned value like this -
doc.data().timestamp = show_year_month_date;
this.dailyreports.push(doc.data());
But it is also not working.
Edit 2
Screenshot of dailyreports
and doc
:
Solution
Instead of trying to update doc.data()
, copy the value to a new variable say data
. Update the timestamp in data
and then push data to this.dailyreports
. And then console log data
to see the object with updated timestamp that was pushed to 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);
});
});
Answered By - abd995
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.