简体   繁体   English

无法读取文档的值 - Firestore

[英]Unable to read the value of a document - Firestore

I have a collection which saves totals datewise .我有一个集合节省总计datewise。

Name of collection is scan_total_dtwise , name of document is company's name, for example Patrol 1 .集合名称为scan_total_dtwise ,文档名称为公司名称,例如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:如果Doc不存在,我可以使用以下代码创建它并将其分配为 1:

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 .但如果它存在,我无法更新它,因为我不知道如何访问incDoc.data().4-AUG-2020

How do I access console.log(incDoc.data().date_doc_to_update);我如何访问console.log(incDoc.data().date_doc_to_update); as it is coming undefined .因为它即将到来undefined See image below console.log("Document data:", doc.data());请参见下图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:如果要使用变量访问对象属性,则应使用 JavaScript 的括号表示法来索引对象:

incDoc.data()[date_doc_to_update]

Or to put in more clearly, IMO:或者更清楚地说,IMO:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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