Reputation: 169
When I'm trying to get data from api with this code:
getUserRole() {
const headers = new Headers();
headers.append('Authorization', `Bearer ${this.getToken()}`);
console.log(this.getToken());
const options = new RequestOptions({ headers: headers});
return this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`).pipe(map(res => res.json()))
.subscribe(data => {
this.userRole = JSON.stringify(data);
if (this.userRole === 'parent') {
this.logout();
} else if (this.userRole === 'school') {
this.isUser = true;
this.isAdmin = false;
this.cookieService.set('isSchool', '1');
} else if (this.userRole === 'admin') {
this.isUser = false;
this.isAdmin = true;
this.cookieService.set('isAdmin', '1');
}
});
}
I get []
, means blank array. But the same API when I try using postman, it gives the array filled like this:
[
"Parent"
]
I doublechecked my api and confirmed, that everything seems ok, but I don't understand why I'm getting different result. Is there any problem with my approach?
Upvotes: 1
Views: 83
Reputation: 222542
Since angular6 has HttpClient by default, you dont have to call .json() explicitly
return this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`).pipe(map(res => res))
also JSON.stringify is unecessary,
this.userRole = data;
EDIT:
You need to pass the headers in the put request aswell,
return this.http.get(`${this.baseURL + this.loginApiRoot}/GetUserRoles`,options)
Upvotes: 1