My user data looks like this which is causing issues...
[{"firstName":"Pinkie","lastName":"Connelly","username":"Darlene.Marvin","email":"[email protected]"},{"firstName":"Easter","lastName":"Ruecker","username":"Giovani.Collier59","email":"[email protected]"},{"firstName":"Harmon","lastName":"Luettgen","username":"Rosanna51","email":"[email protected]"},{"firstName":"Angeline","lastName":"Kunze","username":"Gabriel_Braun69","email":"[email protected]"},{"firstName":"Gayle","lastName":"Bahringer","username":"Dorcas_Roob55","email":"[email protected]"},{"firstName":"Adriana","lastName":"Renner","username":"Serenity.Armstrong42","email":"[email protected]"},{"firstName":"Arvid","lastName":"Kiehn","username":"Estrella87","email":"[email protected]"},{"firstName":"Kristofer","lastName":"Nader","username":"Terence.Walker7","email":"[email protected]"},{"firstName":"Rosa","lastName":"Lebsack","username":"Freida_Hegmann46","email":"[email protected]"},{"firstName":"Rogers","lastName":"Thiel","username":"Mike_Braun","email":"[email protected]"}]
I am using Angular and imported Material Angular. The first problem I had was using *NgFor with this data structure, but since Angular V6 you can now use a keyValue pipe which is really cool! so that problem solved.
I then wanted to *NgFor into a table so each person would populate a row. You dont use NgFor for a table, instead it uses dataSource -
<table mat-table #table [dataSource]="user">
<!-- table rows -->
</table>
But again... Provided data source did not match an array, Observable, or DataSource at getTableUnknownDataSourceError
This is how I am getting user -
export class AppComponent {
error: any;
title = 'toms-app';
user: User;
constructor(private apiServicefile: ApiServicefile) { }
ngOnInit(): void {
this.apiServicefile.getUser()
.subscribe(
(data: User) => this.user = { ...data }, // success path
error => this.error = error // error path
);
}
}
New to typeScript and how to go about getting my desired result. please help?
this.user = { ...data }bythis.users = data. You get an array from the server, and you need this array for your datasource. Why transform it to an object? Renameusertousers, since it's an array of users and not a single User. And thus change its type to Array<User>.