javascript - 在某个函数中包含函数

标签 javascript typescript nestjs

我正在为我的 Angular 色守卫获取一个 json 对象,我正在尝试检查并查看用户是否有一组 Angular 色,其中一个 Angular 色在该操作的可接受 Angular 色列表中。我的功能设置如下:

export class RoleGuard implements CanActivate{
constructor (private readonly reflector: Reflector, readonly jwtService: JwtService) {}
public canActivate(context: ExecutionContext): boolean {
    const roles = this.reflector.get<string[]>('groups', context.getHandler());
    if (!roles) {
        return true;
    }
    const request = context.switchToHttp().getRequest();
    const user: any = this.jwtService.decode(request.headers.authorization.substr(7), { json: true, complete: true });
    // tslint:disable-next-line:ter-arrow-parens
    const hasGroup = () => user.payload.groups.some((role: any) => roles.includes(role));
    return user && user.payload.groups && hasGroup();

user.payload.groups 是用户拥有的 Angular 色数组,“Angular 色”是可以执行该操作的已接受 Angular 色列表。然而,每次程序运行时,即使 Angular 色正确,它也会失败。这里发生了什么我没有看到?我从 nestjs 网站的文档中得到了这个:https://docs.nestjs.com/guards

组对象示例:

Array(3) ["role1", "role2", "role3"]
length:3
__proto__:Array(0) [, …]
0:"role1"
1:"role2"
2:"role3"

Angular 色对象示例:

Array(2) ["role4", "role3"]
length:2
__proto__:Array(0) [, …]
0:"role4"
1:"role3"

最佳答案

所以我发现这个问题与 Angular 色守卫代码无关。这工作正常问题是 Angular 色名称不匹配。我和我的 friend 解决了这些名字,一切正常。我有点不好意思,感谢您提供的所有帮助。

关于javascript - 在某个函数中包含函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52973770/

相关文章:

typescript - 如何在 VS Code 中启用最新的 TypeScript 语言服务?

windows - Angular 4 错误 : No provider for ChildrenOutletContexts in Karma-Jasmine Test

javascript - React 路由器错误(失败的 prop 类型 : Invalid prop `children` supplied to `Switch` , 需要一个 ReactNode。)

javascript - 在表单的文本区域内选择时自动将单词转换为主题标签

javascript - 将 HTML 模板插入到 tinymce 4 编辑器中

javascript - 错误类型错误 : Cannot read property '1' of null

nestjs - 如何在 NestJS Controller 中获取表单数据

sql - 我如何在 NestJS 中使用原始 SQL 而不是 TypeOrm 或 Sequelize?

nestjs - 使用一种配置为 CLI 和 NestJS 应用程序配置 TypeORM

javascript - 溢出-x : hidden; overflow-y: inherit