I made an http call using Angular5 http client. In map function I have done the casting of the response to a PersonModel.
PersonModel has a function getFullName() which returns first_name + last_name
After doing the casting when I try to access this method on casted person object this shows an error of getFullName is not a function.
export class PersonModel {
constructor(
public first_name: string = null,
public middle_name: string = null,
public last_name: string = null) {
}
getFullName() {
return this.first_name + this.last_name;
}
}
This is the service
get(id: number): Observable<PersonModel> {
return this.http.get("customer/" + id)
.map((response: any) => {
return <PersonModel>response;
});
}
customerService.get(1).subscribe(result => {
console.log(result.getFullName());
})
Error core.js:1350 ERROR TypeError: person.getFullName is not a function
Log for object:
{first_name: "kjlkj", last_name: "jnkjh"}
However when I create an object like this:
const person = new PersonModel();
person.first_name = "lkj";
person.last_name= "lkj";
then this logs as this:
PersonModel {first_name: "kjlkj", last_name: "jnkjh"}