I have a simple model:
export class Profile extends ServerData {
name: string;
email: string;
age: number;
}
When I am make a call to the server (Angular 4, $http) I often get the following response:
{
name: string;
email: string;
}
The age
property is missing.
Is there any way to use my model and create a default age in case it is missing? I would prefer not to have to create 2 separate models if possible.
I don't want to create the age as an optional property - I need it, even if it is just with an incorrect default.
UPDATE:
This is the call I making to the server:
results-manager.component.ts:
this.resultsManagerService.getResults(this.config.request.action, this.pagingAndSorting, this.args).subscribe(
results => {
this.results = this.results.concat(results as Profile[]);
results-manager.service.ts:
getResults(action: string, pagingAndSorting: PagingAndSorting, args: string[]): Observable<Profile[]> {
return this.apiService.callServer(
pagingAndSorting,
action,
...args );
}
The request works and I receive the response, but even if I define the default values (as suggested by @msanford's answer) they get removed when I receive the response back in the component. Likewise if i add a constructor to the model (as per Mike Tung's answer).
It seems like the backend response is completely overwriting the model - not just assigning the values.
How can I get it to just assign the values to the model and not remove the values it does not return?