Javascript 找不到命令负载异常的CommandHandler
我尝试使用命令DTO,但无法识别他的处理程序。 当我记录DTO时,它是一个没有签名的简单对象 这是我的控制器: 异步索引(@Body()createUserCommand:createUserCommand):承诺{ log(createUserCommand); return wait this.commandBus.execute(createUserCommand); } 我得到以下输出: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',
{
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);
}