I'm traying to understand why I cannot see in the html using ngFor all the users and there components in a table. It's seams that retain in usersList: User[] the response that is an array list of type User as in the next image PtrSCr of the Web page and the console error but in the end it say that Error: Error trying to diff '[{"id":101,"userName":"tcorneanu","password":"password","email":"tcorneanu@gmail.com"},{"id":104,"userName":"user3","password":"pwd3","email":"user3@gmail.com"}]'. Only arrays and iterables are allowed HTML
<h2>User list</h2>
{{usersList}}
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>index</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let hero of usersList; let i = index">
<td>{{hero.id}}</td>
<td>{{i}}</td>
</tr>
</tbody>
</table>
TS
usersList: User[] = [];
private getUsersList(token:any):void{
this.userService.getUsersList(token).subscribe(data => {
console.log("data is: "+data),
this.usersList = data
}, (error) => {
console.log(error);
});
}
userService
getUsersList(token:any): Observable<User[]>{
let tokenStr = 'Bearer '+token;
const headers = new HttpHeaders().set("Authorization",tokenStr);
return this.httpClient.get<User[]>(`${this.url}/users`,{headers, responseType: 'text' as 'json'});
}
User
export interface User {
id: number;
userName: string;
password: string;
email: string;
}