I have the code shown below. When I try to set data returned from a service (the assetGroup variable) the value being set is null. My best bet is it is because not all nested arrays are typed, and needs to be mapped. So my question is two fold, is this the reason why the object continue being null after I set it? And if yes can you somehow do an assign that makes a deep mapping or whats the best approach here?
export class Asset {
constructor(obj: any) {
Object.assign({}, obj);
}
assetId:number;
assetSubGroupId:number;
code: number;
name: string;
}
export class AssetGroup {
constructor(obj: any) {
Object.assign(this, obj);
}
assetGroupId:number;
code: number;
name: string;
assetSubGroupList:Asset[];
}
export class AssetSubGroup {
constructor(obj: any) {
Object.assign(this, obj);
}
assetSubGroupId:number;
assetGroupId:number;
code: number;
name: string;
assetList: Asset[];
}
This is my service:
getAssetGroup(id:number):Observable<AssetGroup>{
var url = `http://xxx.azurewebsites.net/api/assetgroups/${id}`;
return this.httpClient.get<AssetGroup>(url).pipe(map(x => new AssetGroup(x)));
}
My component:
private assetGroup: AssetGroup;
loadAssets(id)
{
this.apiService.getAssetGroup(id).subscribe((data) => {
this.assetGroup = data;
console.log(this.assetGroup); // <---- assetGroup is NULL?
});
}