简体   繁体   English

错误:错误错误:未捕获(承诺):[对象布尔]

[英]Error : ERROR Error: Uncaught (in promise): [object Boolean]

When there is no session when I go on an incognito tab and then redirect or open URL http://localhost:4200/#/ it returns me error ERROR Error: Uncaught (in promise): [object Boolean]当我进入隐身选项卡然后重定向或打开 URL http://localhost:4200/#/ 时没有会话时,它会返回错误错误:未捕获(承诺):[对象布尔]

when I open the URL it should go directly to login page , what causes the error ?当我打开 URL 时,它应该直接进入登录页面,是什么导致了错误? any idea?任何想法? is this because of the get session ?这是因为获取会话吗? 在此处输入图片说明

#getsession #getsession

 public getSession(): Promise<boolean> {
    const session = localStorage.getItem(tokenName);
    return new Promise((resolve, reject) => {
      if (session) {
        return resolve(true);
      } else {
        return reject(false);
      }
    });
  }

#Code #代码

 private checkPermission(allowedUserRoles: Roles[]): Promise<boolean> {
    return this.authService.getSession().then((session: boolean) => {
      if (session && this.authService.isTokenValid) {
        this.route.navigateByUrl('/login');
        if (!allowedUserRoles) {
          this.route.navigateByUrl('/login');
          return true;   // if no user roles has been set, all user are allowed to access the route
        } else {
          return this.authService.getUserRoles().then((userRoles: string[]) => {
            if (this.authService.areUserRolesAllowed(userRoles, allowedUserRoles)) {
              return true;
            } else {
              this.route.navigateByUrl('/transactions');
              return false;
            }
          });
        }
      } else { 
        this.route.navigateByUrl('/login');
        return false ;
      }
    });
  }

#code 2 #代码2

constructor(private authService: AuthService, private route: Router,) {}

  public async canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
    const allowedUserRoles = this.getRoutePermissions(route);
    return await this.checkPermission(allowedUserRoles);
  }

#getting token #获取令牌

 isTokenValid(){
    if(localStorage.getItem(tokenName)){      
      return true;
    }
    return false;
  }

When there is no session when I go on an incognito tab and then redirect or open URL http://localhost:4200/#/ it returns me error ERROR Error: Uncaught (in promise): [object Boolean]当我进入隐身选项卡然后重定向或打开 URL http://localhost:4200/#/ 时没有会话时,它会返回错误错误:未捕获(承诺):[对象布尔]

when I open the URL it should go directly to login page , what causes the error ?当我打开 URL 时,它应该直接进入登录页面,是什么导致了错误? any idea?任何想法? is this because of the get session ?这是因为获取会话吗? 在此处输入图片说明

#getsession #getsession

 public getSession(): Promise<boolean> {
    const session = localStorage.getItem(tokenName);
    return new Promise((resolve, reject) => {
      if (session) {
        return resolve(true);
      } else {
        return reject(false);
      }
    });
  }

#Code #代码

 private checkPermission(allowedUserRoles: Roles[]): Promise<boolean> {
    return this.authService.getSession().then((session: boolean) => {
      if (session && this.authService.isTokenValid) {
        this.route.navigateByUrl('/login');
        if (!allowedUserRoles) {
          this.route.navigateByUrl('/login');
          return true;   // if no user roles has been set, all user are allowed to access the route
        } else {
          return this.authService.getUserRoles().then((userRoles: string[]) => {
            if (this.authService.areUserRolesAllowed(userRoles, allowedUserRoles)) {
              return true;
            } else {
              this.route.navigateByUrl('/transactions');
              return false;
            }
          });
        }
      } else { 
        this.route.navigateByUrl('/login');
        return false ;
      }
    });
  }

#code 2 #代码2

constructor(private authService: AuthService, private route: Router,) {}

  public async canActivate(route: ActivatedRouteSnapshot): Promise<boolean> {
    const allowedUserRoles = this.getRoutePermissions(route);
    return await this.checkPermission(allowedUserRoles);
  }

#getting token #获取令牌

 isTokenValid(){
    if(localStorage.getItem(tokenName)){      
      return true;
    }
    return false;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM