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

怎甘沉沦 提交于 2019-12-13 20:26:36

问题


I'm trying to display the data on the front end but i get the above error. How to resolve this?

service.ts

rec_source(){
    var headers = new HttpHeaders().set('Authorization', 'Token ' + localStorage.getItem('usertoken'));
    headers.append('Accept', 'application/json');
    var options =  {
      headers: headers
  };
  return this.httpClient.get('api/recruit/fetch_recruitment_details',options)
  }

component.ts

sources:any = [];

constructor(private Authentication:AuthService) { }

ngOnInit() {

this.Authentication.rec_source()
  .subscribe(source => {
    this.sources = source;
  });
}

component.html

<table *ngIf="sources" class="row-border hover">
<thead>
  <tr>
    <th>Data</th>
  </tr>
</thead>
<tbody>
  <tr *ngFor="let source of sources">
    <td>{{ source}}</td>
  </tr>
</tbody>
</table>

//the data that needs to be displayed

//the error


回答1:


Your api response is not an array or iterable, that what the error says. It is object, which is not iterable with *ngFor. Maybe you wanted to iterate data_candidate_source or data_new_hiredate. In such case you need to assign correct property to your source property in component:

this.sources = source.data_candidate_source

Or when you want to iterate through object then you can use newly added keyvalue pipe.



来源:https://stackoverflow.com/questions/53044045/error-trying-to-diff-object-object-only-arrays-and-iterables-are-allowed-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!