简体   繁体   中英

Unable to read the value of a document - Firestore

I have a collection which saves totals datewise .

Name of collection is scan_total_dtwise , name of document is company's name, for example Patrol 1 . In this document I am setting records datewise, see pic below:

在此处输入图片说明

If Doc does not exist, I am able to create it and assign it number 1, using below code:

firebase.firestore().collection("scan_total_dtwise").doc(this.splitted_1).set({
  [this.show_year_month_date] : 1,

But if it exists, I am not able to update it, because I dont know how to access incDoc.data().4-AUG-2020 .

How do I access console.log(incDoc.data().date_doc_to_update); as it is coming undefined . See image below console.log("Document data:", doc.data()); :

update_exisiting_serial_total(){
  var date_doc_to_update = this.show_year_month_date
  var docRef = firebase.firestore().collection('scan_total_dtwise').doc(this.splitted_1);
  firebase.firestore().runTransaction(transaction=> {
    return transaction.get(docRef).then(incDoc=> { 
        //if no value exist, assume it as 0 and increase to 1
        console.log(incDoc.data().date_doc_to_update);
        var newIncId = (incDoc.data().date_doc_to_update || 0) + 1;
        transaction.update(docRef, { [this.show_year_month_date]: newIncId });
        return newIncId;
        ...//more code

在此处输入图片说明

If you want to access an object property using a variable, you should use JavaScript's bracket notation to index into the object:

incDoc.data()[date_doc_to_update]

Or to put in more clearly, IMO:

const data = incDoc.data();
value = data[date_doc_to_update];

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