小编mad*_*ack的帖子

在 NestJS 上使用管道为查询字符串参数提供默认值

我正在尝试实现这个例子。看起来实现起来很简单,但我遇到了一些麻烦。

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

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

nestjs

5
推荐指数
0
解决办法
2745
查看次数

标签 统计

nestjs ×1