简体   繁体   中英

Push data in array but after converting timestamp to date

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 :

在此处输入图片说明

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); }); });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM