将Json数据从服务器存储到Angular 2中的数组中

我使用nodejs服务器从SQL数据库中获取数据。

我会将数据存储在Tache数组中:

getTaches(): Observable<Tache[]> { return this.http.get(this.tachesUrl) .map(response => { this.taches = response.json().data as Tache[]; console.log(this.taches); }) .catch(this.handleError); } 

当我在控制台上打印时,我得到的结果如下:

 undefined 

当我打印response.json()我得到我的价值观:

 Object {taches: Array(8)} 

所以我删除.data,然后再试一次,然后我在模板文件的第一行得到另一个错误:

  ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed 

这是我的html文件:

  <md-toolbar color="primary"> <span>Taches non traitées</span> </md-toolbar> <md-card> <md-list > <ng-container *ngFor="let tache of this.tacheService.taches " > <md-list-item *ngIf="tache.stat == 0" (click)="onSelect(tache)" [class.selectionnee]="tache === tacheSelectionnee">{{tache.stat}}+{{tache.id}} + {{tache.name}} <div *ngIf="tache === tacheSelectionnee"> <button md-icon-button [mdMenuTriggerFor]="menu"> <md-icon>more_vert</md-icon> </button> <md-menu #menu="mdMenu"> <button md-menu-item (click)="openDialog(tache)"> <md-icon>edit</md-icon> <span>Modifier</span> </button> <button md-menu-item (click)="delete(tache)"> <md-icon>delete</md-icon> <span>Supprimer</span> </button> <button md-menu-item (click)="save(tache)"> <md-icon>cached</md-icon> <span>Traiter</span> </button> </md-menu> </div> </md-list-item> </ng-container> </md-list> </md-card> 

我会正确地将数据存储在taches数组中,以便我可以将它们显示为待办事项列表。

尝试这个:

 getTaches(): Observable<Tache[]> { return this.http.get(this.tachesUrl) .map(response => { this.taches = response.json().taches as Tache[]; console.log(this.taches); }) .catch(this.handleError); } 

你从api收到的对象持有taches数组