我如何遍历Firestore中的对象数组(数组联合对象)

时间:2019-11-29 02:54:27

标签: angular typescript ionic-framework frameworks

我有一个注释数组字段,在Firestore数据库中有两个对象。

enter image description here

它在控制台中返回此值:

enter image description here

我试图用下面的代码遍历数组,但是没有运气。我究竟做错了什么?

HTML:

<ion-card style="margin-top: 10px !important;" *ngFor ="let comment of comments">   
    <ng-container *ngFor="let item of comment?.comments">
       <div style="color: black"><h6>{{item?.comment}}</h6></div>

    </ng-container>
</ion-card>

TS:

comments: any = [];

getData(){
  this.route.data.subscribe(routeData => {
    let data = routeData['data'];
    this.comments = [];
    if (data) {
      this.item = data;
      this.image = this.item.image;  
      this.comments.push(this.item.comments)
      console.log( this.comments ) 
    }
  })   
}

1 个答案:

答案 0 :(得分:0)

似乎您应该这样做:

<ion-card style="margin-top: 10px !important;" *ngFor="let comment of comments">   
    <ng-container *ngFor="let item of comment">
       <div style="color: black"><h6>{{item?.comment}}</h6></div>

    </ng-container>
</ion-card>

因此,comment而不是内部循环中的comment?.comments