【问题标题】:Nest.js - is there another way to get request/body but by ParamDecorator?Nestjs - 除了Param Decorator,还有另一种获取请求/正文的方法吗?
【发布时间】:2019-04-25 16:56:26
【问题描述】:

不知何故,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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-08-31
        • 2012-10-01
        • 2015-08-25
        • 1970-01-01
        • 1970-01-01
        • 2022-12-24
        • 2019-10-16
        • 1970-01-01
        • 2018-03-23
        相关资源
        最近更新 更多