繁体   English   中英

我可以显示来自 firestore 集合的数据,但无法访问数组元素

[英]I can display data from a firestore collection, but cannot access the array elements

我可以从 firestore 中提取 stream 数据作为可观察数据并显示该数据。 但是,我找不到任何方法来访问该数据以对其执行任何操作。 这很好,因为数据会自动更新,但我看不到在可观察结果中访问数据的方法。

比如我通过这个function到一个服务中得到stream的数据。

Function:

this.projectList = this.FirebaseService.getCollection('projects', 'creatorId', this.userData.uid);

服务:

getCollection( collection, paramName, paramValue ) {
    return this.afs.collection( collection, ref => ref.where( paramName, '==', paramValue )).valueChanges();
}

返回的集合可以显示在列表中或用于下拉选择,如下所示。

<mat-form-field class="w-80-p ml-10-p mt-20">
  <mat-label>Select a version to see the details</mat-label>
  <mat-select  (selectionChange)="onVersionSelected()"
      [(ngModel)]="currentProject.currentVersionId">
       <mat-option *ngFor="let version of (currentProject['versions'] | async); index as i" [value]="version.uid">
        {{i+1}} - {{version.name}}
      </mat-option>
  </mat-select>
</mat-form-field>

function 处理可观察数据:

onVersionSelected(): void
{
    this.currentProject.versions.forEach((thisVer, index) => {
        if (thisVer.id == this.currentProject.currentVersionId){ this.currentVersion=thisVer; }
    });

}

问题是使用 for 循环,我仍然得到一个可观察的而不是数组中的实际数据。 我如何访问这些数据?

您可以尝试执行以下操作:

this.FirebaseService.getCollection(
    'projects', 'creatorId', this.userData.uid).subscribe(data=>{
        var a=data;
    }
);

暂无
暂无

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

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