I am working with a service class in Angular that utilizes the HttpClient
to retrieve data from a web service. The web service responds with a JSON object structured like this:
{
"id": "some type of user id",
"name": "The name of the user",
"permissions": [
"Array of permission names as strings"
]
}
The goal is to have the HttpClient
's get<>
method return these objects typed as instances of my custom DomainUser
class.
This is what the DomainUser
class looks like:
export class DomainUser {
public id: string;
public name: string;
public permissions: string[];
public get isAdmin(): boolean {
return this.permissions.includes('admin permission's name');
}
}
However, when trying to access the value of the isAdmin
property of an instance of DomainUser
, it shows as undefined. How can I correctly obtain the user data typed as DomainUser
?
The get
call is made like this:
this.http.get<DomainUser>(this.apiUrl + '/Users/Self').subscribe(
next => {
console.info('User: "%s" (Admin: %s)', next.name, next.isAdmin ? 'yes' : 'no');
// ...
},
error => {
// handle errors
}
);