Currently, I have a backend REST service that is responsible for returning a string:
@GetMapping("/role/{id}")
public String findRole (@PathVariable("id") String username) {
User user = userRepository.findByUsername(username);
return user.getRoles().get(0).getName();
}
I am attempting to retrieve this string and utilize it in an Angular .ts file. Here is my service code:
getRole(name: string): Observable<any> {
return this.http.get('http://localhost:8080/api/role/' + name);
}
This is my .ts code:
login(){
this.loginService.authenticate(this.credentials,()=>{
this.loginService.getRole(this.credentials.username).subscribe(
data => {
this.role=JSON.stringify(data);
});
if (this.role == "ROLE_ADMIN" )
{ this.router.navigateByUrl('/home/(contentOutlet:product)');}
else console.log(this.role);
/*
else
{ this.router.navigateByUrl('/home/(contentOutlet:dashboard)')}*/
})
}
However, I am encountering the following error:
"Unexpected token R in JSON at position 0"