Angular Firestore-获取文档数据并分配给组件内部的变量

问题描述

我正在使用Angular Firestore来获取和过滤时间戳上的@R_502_6274@。

唯一的问题是,每当我获得doc.data()时,都无法将其分配给变量,以便可以在组件内部的任何位置使用此变量。

Component.ts

>>> import decimal 
>>> x = decimal.Decimal('3.4')
>>> print(x - math.floor(x))
Decimal('0.4')

我这样分配了wilsData:

joinData(toData,fromData){
    let toDate = toData
    let fromDate = fromData
    console.log("valuetoDate : ",toDate)
    let dateto = new Date(toDate).getTime() / 1000;
    let dateFrom = new Date(fromDate).getTime() / 1000;
    console.log("dateto :",dateto)


          let collection = this.fstore.firestore.collection('data_reports').where('timestamp','>=',dateFrom).where('timestamp','<=',dateto);
          collection.get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
              console.log("data: ",doc.data()) // ---> this line I can get the doc.data()
              this.wilsData = doc.data();       // ---> this line the value of wilsData is undefined
              console.log("wilsData: ",this.wilsData)
            })
          }); 
    
  }

登录时得到 private wilsData: any[]; 值。

问题是,如何将doc.data()分配给变量?

感谢您的帮助

解决方法

您需要初始化数组:

 private wilsData: any[] = [];