My model is pretty straightforward:
export class Profile extends ServerData {
name: string;
email: string;
age: number;
}
Whenever I make a server call using Angular 4's $http, I consistently receive this response:
{
name: string;
email: string;
}
The property age
is missing.
Is there a way to incorporate my model and set a default age if it's not included? I'd rather avoid creating separate models if possible.
I don't want to make the age an optional property - I require it, even if it means having an incorrect default.
UPDATE:
This is the request I'm sending 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 goes through successfully and I get the response, but any default values I define (as suggested by @msanford's answer) are removed when I receive the response in the component. Similarly, adding a constructor to the model (like Mike Tung's answer) doesn't work either.
It seems like the backend response completely overrides the model instead of just assigning the values.
How can I ensure that it only assigns the values to the model without removing any existing ones?