Nestjs 与DTO相比,嵌套js中的DTO接受更多参数
我有一个DTONestjs 与DTO相比,嵌套js中的DTO接受更多参数,nestjs,Nestjs,我有一个DTO export class UpdateUserRoleDTO { @ApiProperty() @IsNotEmpty() readonly userId:number; @ApiProperty() @IsNotEmpty() @IsNumber() readonly roleId: number; } 我的控制器看起来像这样 @UsePipes(new ValidationPipe()) @Post('/update') async
export class UpdateUserRoleDTO {
@ApiProperty()
@IsNotEmpty()
readonly userId:number;
@ApiProperty()
@IsNotEmpty()
@IsNumber()
readonly roleId: number;
}
我的控制器看起来像这样
@UsePipes(new ValidationPipe())
@Post('/update')
async updateUser(@Body() updateUserDto: UpdateUserDTO): Promise<User> {
return await this.userService.updateUser(updateUserDto);
}
它击中了我的服务文件。我想避免这种情况。请确保只传递DTO中提到的参数,否则它会给出400给定您的代码,我会将设置为true的选项传递给您正在实例化的
验证管道,就像在控制器中一样:
控制器
@UsePipes(新的ValidationPipe({whitelist:true}))
@发布(“/update”)
异步updateUser(@Body()updateUserDto:updateUserDto):承诺{
返回wait this.userService.updateUser(updateUserDto);
}
这应该可以完成工作
如果有帮助,请告诉我,否则请毫不犹豫地发表评论并分享您的发现;) @UsePipes(new ValidationPipe({bankinnon-whitelisted:true,whitelist:true})将此装饰程序添加到您的控制器只是两个方面的说明:不需要等待服务,而只需返回承诺,并且您通常不需要在API中使用名为“/update”的路由,因为您已经拥有http请求方法“post”区分行动。
payloadObj = {
userId : 1,
roleId : 1,
xyz : 'assddcds',
someotherkey : 'fsdvs'
}