【问题标题】:Angular Firestore - Get document data and assign to variable inside the componentAngular Firestore - 获取文档数据并分配给组件内的变量
【发布时间】:2020-12-02 21:08:40
【问题描述】:

我正在使用 Angular Firestore 根据时间戳获取和过滤数据库。

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

组件.ts

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

我这样分配 wilsData:

  private wilsData: any[];

我在登录时得到undefined 值。

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

感谢您的帮助

【问题讨论】:

    标签: angular firebase google-cloud-firestore angularfire


    【解决方案1】:

    你需要初始化数组:

     private wilsData: any[] = [];
    

    【讨论】:

    • 仍然,我不能将它分配给这个 wilsData :(
    • 你得到相同的undefined值行为吗?
    猜你喜欢
    • 2018-08-10
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2018-07-30
    • 1970-01-01
    • 2018-12-19
    相关资源
    最近更新 更多