javascript 在nestjs保护中使用路径参数

qoefvg9y  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(119)

除了从http上下文中查找原始请求对象之外,我可以使用其他方法在NestJS保护函数中获取路径参数吗?
例如我想做的是

@Patch(':id/someActionName')
@UseGuards(SomeGuard)
async activateRole(@Param('id') id, @Body() input: SomeObject): Promise < any > {
    //some logic
    return response;
}

我的SomeGuard将获取'id'参数和'input'参数的值,输入参数很简单,但我看不到获取'id'的简单方法

m4pnthwp

m4pnthwp1#

在guard中,您可以通过从上下文获取请求来访问路由参数,如下所示:

canActivate(context: ExecutionContext): boolean {
  const request = context.switchToHttp().getRequest();
  const params = request.params;
  const id = params.id; // automatically parsed
}

这在文档中没有,我遇到了和您完全相同的问题,不得不深入研究请求对象。
请注意,Guards are executed after all middleware, but before any interceptor or pipe.https://docs.nestjs.com/guards

相关问题