Nest.js - 是否有另一种获取请求/正文但通过ParamDecorator的方法?

问题描述 投票:0回答:2

不知何故,ParamDecorator看起来完全不酷。是否有其他方式来获取身体或要求?例如。通过函数装饰器或全局的东西?

@Post('token')
public async createToken(@Body() body: UserLogin): Promise<any> {
  return await this.authService.signIn(body);
}

不幸的是,这不起作用。

@Post('token')
@Body('body')
public async createToken(body: UserLogin): Promise<any> {
  return await this.authService.signIn(body);
}
typescript nestjs
2个回答
1
投票

如果不在函数参数本身上使用装饰器,就没有办法提取这些信息。是什么让你觉得他们“不酷”?通过将它们从函数参数移动到函数,您似乎并没有获得太多收益。


0
投票

您可以使用请求对象访问正文

async getPostById(@Req() req, @Res() res) {
   const body = req.body;
   ...
}

但是这样你可能会要求你使用装饰器来访问请求对象!

© www.soinside.com 2019 - 2024. All rights reserved.