未收到对象时比较值(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.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)

我不是Aurelia开发人员,但这似乎是一个简单的JS问题,除非我错了。 假设错误在"isUserInRole"中,您可以这样做。

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

基本上,在检查其角色之前,只需检查您的用户身份是否存在。

希望这有帮助!

我解决了它,为什么:

 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.

相关内容

  • 没有找到相关文章

最新更新