Angular:TypeError:无法读取属性'然后'未定义的

时间:2018-05-20 09:54:58

标签: angular typescript jhipster

我正在使用JHipster Auth-guard。我试图为没有路由权限的用户触发canActivate。

我不能为我的生活看到它出错的地方。控制台说.then()在下面的代码中未定义。我试过控制台记录主体,它没有未定义。伙计们,谁能看到我做错了什么?

intentPurple = findViewById(R.id.button5);
Intent gg = new Intent(MainActivity.this, PurpleActivty.class);
ButtonListener b = new ButtonListener(gg, MainActivity.this);
intentPurple.setOnClickListener(b);

我的代码:

checkLogin(authorities: string[], url: string): Promise<boolean> {
    var principal = this.principal;

    return Promise.resolve(principal.identity().then((account) => {



   ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined
    at AuthGuard.checkLogin (auth-guard.ts:27)
    at AuthGuard.canActivate (auth-guard.ts:21)

的Auth-guard.ts

app.module

    const routes: Routes = [
    {
        path: '**',
        component: DashboardComponent,
        canActivate: [AuthGuard],
        resolve: {
            data: DashboardService
        }
    }
];

@NgModule({
    declarations: [
        DashboardComponent
    ],
    imports: [
        RouterModule.forChild(routes),
        HttpClientModule,
        HttpModule,
        CdkTableModule,
        MatButtonModule,
        MatDividerModule,
        MatFormFieldModule,
        MatIconModule,
        MatMenuModule,
        MatSelectModule,
        MatSidenavModule,
        MatTableModule,
        MatTabsModule,

        NgxChartsModule,
    ],
    providers: [
        ProjectDashboardService, AuthGuard, Principal, StateStorageService
    ]
})

principal service.ts

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';

import { Principal } from './principal.service';
import { StateStorageService } from './state-storage.service';

@Injectable()
export class AuthGuard implements CanActivate {

    constructor(private router: Router,
        private principal: Principal,
        private stateStorageService: StateStorageService) {
    }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Promise<boolean> {

        const authorities = route.data['authorities'];
        // We need to call the checkLogin / and so the principal.identity() function, to ensure,
        // that the client has a principal too, if they already logged in by the server.
        // This could happen on a page refresh.
        return this.checkLogin(authorities, state.url);
    }

    checkLogin(authorities: string[], url: string): Promise<boolean> {
        var principal = this.principal;

        return Promise.resolve(principal.identity().then((account) => {

            if (!authorities || authorities.length === 0) {
                return true;
            }

            if (account) {
                return principal.hasAnyAuthority(authorities).then((response) => {
                    if (response) {
                        return true;
                    }
                    return false;
                });
            }

            this.stateStorageService.storeUrl(url);
            //this.router.navigate(['accessdenied']);
            window.location.href = "http://stackoverflow.com";
            return false;
        }));
    }
}

1 个答案:

答案 0 :(得分:0)

感谢您的帮助@ user184994。当我返回虚假时它起作用了。解决方案:更改

中的回报
  

principal.identity()

如果userIdentity为false并删除此

,则返回false
  

if(!authorities || authorities.length === 0){return true; }