I'm encountering an issue with my TypeScript code:
SyntaxError: Unexpected token u in JSON at position 0:
Upon checking the browser console, I found this error message: https://i.sstatic.net/mk2Z4.png
Here's the problematic TypeScript code:
roleMatch(allowedRoles): boolean {
var isMatch = false;
var userRoles: string[] = JSON.parse(localStorage.getItem('userRoles'));
allowedRoles.forEach(element => {
if (userRoles.indexOf(element) > -1) {
isMatch = true;
return false;
}
});
return isMatch;
}
Another incorrect part of the code:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
if (localStorage.getItem('userToken') != null) {
let roles = next.data["roles"] as Array<string>;
if (roles) {
var match = this.userService.roleMatch(roles);
if (match) return true;
else {
this.router.navigate(['/forbidden']);
return false;
}
}
else
return true;
}
this.router.navigate(['/login']);
return false;
}