Сравнить значения, когда объект не получен (TypeScript, Aurelia) - PullRequest
1 голос
/ 23 апреля 2019

Я пытаюсь сравнить определенную 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.log(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)

Ответы [ 2 ]

2 голосов
/ 23 апреля 2019

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

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

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

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

1 голос
/ 24 апреля 2019

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

 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.

...