【问题标题】:Unable to read the value of a document - Firestore无法读取文档的值 - Firestore
【发布时间】:2020-12-17 18:14:52
【问题描述】:

我有一个收藏datewise的收藏。

集合名称为scan_total_dtwise,文档名称为公司名称,例如Patrol 1。在本文档中,我按日期设置记录,请参见下图:

如果Doc 不存在,我可以使用以下代码创建它并将其分配为 1:

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

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

console.log(incDoc.data().date_doc_to_update); 即将到来时如何访问它undefined。见下图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

【问题讨论】:

    标签: javascript typescript firebase google-cloud-firestore


    【解决方案1】:

    如果要使用变量访问对象属性,则应使用 JavaScript 的括号表示法来索引对象:

    incDoc.data()[date_doc_to_update]
    

    或者更清楚地说,IMO:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2020-11-03
      • 2021-09-25
      • 2020-11-14
      • 2019-06-07
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多