在NestJS上使用管道为querystring参数提供默认值

时间:2020-06-23 08:37:14

标签: nestjs

我正在尝试实现this示例。它看起来实现起来很简单,但是我遇到了一些麻烦。

@Get('test')
async test(
  @Query('size', new DefaultValuePipe(10), ParseIntPipe) size: number,
  @Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
) {
   // Do some stuff
}
  • 我提出的第一个请求是http://localhost/api/test
    • 我发送的不带参数的请求将回复400 Bad Request,消息为Validation failed (numeric string is expected)
  • 我提出的第二个请求是http://localhost/api/test?size=&page=
    • 我使用空参数发送的请求以0的形式传递,而不是默认情况下要提供的值。

在两个请求中,默认值都希望通过,对吗?如果是这样,我在做什么错了?

0 个答案:

没有答案