Сравните значения, когда объект не получен (TypeScript, Aurelia)

Пытаюсь сравнить заданную role (по моему app.ts) и возрожденную роль из WebServer. Но когда я не вошел в систему, у меня проблема со сравнением значений:

Вот что я делаю:

export class RoutingAuthorizeStep {

 public readonly userIdentity: UserIdentityModel;

 constructor (userIdentity: UserIdentityModel) {
    this.userIdentity = userIdentity;}

  run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {

  let requiredRoles = navigationInstruction.getAllInstructions()
                    .map(i => i.config.settings.roles)[0] as string[];
                    //requiredRoles is example 'superUser'

  let isUserInRole = requiredRoles?
    requiredRoles.some(r => r === this.userIdentity.role) : true;
  }
 }

Когда я проверил отладку: console.info(this.userIdentity.role);

У меня есть это сообщение:

aurelia-logging-console.js?dc89:45 ERROR [app-router] TypeError: 
                                                  Cannot read property 'role' of undefined
at RoutingAuthorizeStep.run (routing-authorize-step.ts?008f:30)
at next (aurelia-router.js?e32b:433)

1
111
2

Ответы:

Решено

Я не разработчик Aurelia, но это кажется простой проблемой JS, если я не ошибаюсь. предполагая, что ошибка находится в «isUserInRole», вы можете это сделать.

  let isUserInRole = requiredRoles?
    requiredRoles.some(r => this.userIdentity && r === this.userIdentity.role) : true;
  }

В основном просто проверьте, существует ли ваш userIdentity, прежде чем проверять его роль.

Надеюсь это поможет!


Я решаю это, почему:

 run(navigationInstruction: NavigationInstruction, next: Next) : Promise<any> {
  if (this.userIdentity == null)
  {
    //return to login
  }
  else
  {
     let requiredRoles: string;
     requiredRoles = navigationInstruction.getAllInstructions()
                                          .map(i => i.config.settings.roles)[0];    
      if (requiredRoles === this.userIdentity.role)
       {        
        return next();
       }
        //return to login
   }
}

Это работает. Но все же requiredRoles.some есть проблема - может быть, какая-то library is missing.