Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.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
Javascript 找不到命令负载异常的CommandHandler_Javascript_Node.js_Typescript_Nestjs_Class Transformer - Fatal编程技术网

Javascript 找不到命令负载异常的CommandHandler

Javascript 找不到命令负载异常的CommandHandler,javascript,node.js,typescript,nestjs,class-transformer,Javascript,Node.js,Typescript,Nestjs,Class Transformer,我尝试使用命令DTO,但无法识别他的处理程序。 当我记录DTO时,它是一个没有签名的简单对象 这是我的控制器: 异步索引(@Body()createUserCommand:createUserCommand):承诺{ log(createUserCommand); return wait this.commandBus.execute(createUserCommand); } 我得到以下输出: { firstName: 'xxx', lastName: 'xxx',

我尝试使用命令DTO,但无法识别他的处理程序。 当我记录DTO时,它是一个没有签名的简单对象

这是我的控制器:

异步索引(@Body()createUserCommand:createUserCommand):承诺{ log(createUserCommand); return wait this.commandBus.execute(createUserCommand); } 我得到以下输出:

 { 
    firstName: 'xxx',
    lastName: 'xxx',
    email: 'xxx@xxx.com',
    password: 'xxx'
}
当我尝试直接使用命令时,它正在工作:

const命令=新建CreateUserCommand();
command.firstName='xxx';
command.lastName='xxx';
command.email=xxx@xxx.com';
command.password='xxx';
return wait this.commandBus.execute(createUserCommand);
以下输出:

 { 
    firstName: 'xxx',
    lastName: 'xxx',
    email: 'xxx@xxx.com',
    password: 'xxx'
}
CreateUserCommand{
名字:“xxx”,
姓氏:“xxx”,
电邮:'xxx@xxx.com',
密码:“xxx”
}

可以将DTO用作命令处理程序吗?

如果使用
@Body
它将生成一个普通的javascript对象,但不会生成DTO类的实例。您可以使用及其
plainToClass(CreateUserCommand,CreateUserCommand)
方法来实际创建类的实例

如果您正在使用,如果您传递选项
transform:true
,它可以自动将普通对象转换为类:

@UsePipes(new ValidationPipe({ transform: true }))
async index(@Body() createUserCommand: CreateUserCommand): Promise<User> {
    console.log(createUserCommand);
    return await this.commandBus.execute(createUserCommand);
}
@UsePipes(新的ValidationPipe({transform:true}))
异步索引(@Body()createUserCommand:createUserCommand):承诺{
log(createUserCommand);
return wait this.commandBus.execute(createUserCommand);
}