Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何过滤掉多余的模型属性_Node.js_Typescript_Nestjs - Fatal编程技术网

Node.js 如何过滤掉多余的模型属性

Node.js 如何过滤掉多余的模型属性,node.js,typescript,nestjs,Node.js,Typescript,Nestjs,我有这样一个DTO对象: export class CreateProductDTO { readonly _id: number; readonly _name: string; readonly _price: number; } DTO用于我的post方法 @Post('users') async addUser(@Response() res, @Body(new ValidationPipe()) createUserDTO: CreateUserDTO) { a

我有这样一个DTO对象:

export class CreateProductDTO {
  readonly _id: number;
  readonly _name: string;
  readonly _price: number;
}
DTO用于我的post方法

@Post('users')
async addUser(@Response() res, @Body(new ValidationPipe()) createUserDTO: CreateUserDTO) {
    await this.userService.addUser(createUserDTO).subscribe((users) => {
        res.status(HttpStatus.OK).json(users);
    });
}
当我发布json数据时,它将序列化为CreateProduceDTO对象

{
  "_id":1,
  "_name":"Lux",
  "_age":19
}
但我发布了带有多余属性的json数据,它还序列化为CreateProduceDTO带有多余属性的ObjJet

{
  "_id":1,
  "_name":"Lux",
  "_age":19,
  "test":"abcv"
}

CreateUserDTO { _id: 1, _name: 'Lux', _age: 19, test: 'abcv' }
我试着用管子过滤,但我不知道。
谢谢大家。

如果您只想删除多余的属性,可以像这样使用ValidationPipe:

new ValidationPipe({whitelist: true})
如果您希望在存在任何非白名单属性时引发错误:

new ValidationPipe({whitelist: true, forbidNonWhitelisted: true})

查看更多选项

如果您只想删除多余的属性,您可以像这样使用ValidationPipe:

new ValidationPipe({whitelist: true})
如果您希望在存在任何非白名单属性时引发错误:

new ValidationPipe({whitelist: true, forbidNonWhitelisted: true})
查看更多选项