我在NodeJS应用程序中有一个接收 DTO 的服务,然后将dto其转换entity为持久性。但是当我尝试保存这个实体时,它会抛出一个错误。
我是这个 ORM 的新手,怀疑我没有正确创建实体对象。我尝试了很多谷歌搜索,但结果很奇怪,这无济于事。
async create(dto: UserDto): Promise<User> {
console.log(dto);
const entity = new User();
entity.name = dto.name;
entity.email = dto.email;
entity.password = dto.password;
entity.gender = dto.gender;
console.log(entity);
return this.userRepository.create<User>(entity);
}
我不想使用任何类型的(自动)映射器。我想手动但正确地执行此操作。
---------- DTO 值------------
{
name: 'jon travolta',
password: '$2b$10$69BOeoRUAF2cyObIsmeMSeepYVC3E.hm1xauCGysgHL/SNl57xDlS',
email: 'jon@travolta.com',
gender: 'male'
}
----------实体值------------
User {
dataValues: {
id: null,
name: 'jon travolta',
email: 'jon@travolta.com',
password: '$2b$10$69BOeoRUAF2cyObIsmeMSeepYVC3E.hm1xauCGysgHL/SNl57xDlS',
gender: 'male'
},
_previousDataValues: {
name: undefined,
email: undefined,
password: undefined,
gender: undefined
},
_changed: Set(4) { 'name', 'email', 'password', 'gender' },
_options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
isNewRecord: true
}
-----------------控制台错误----------------
[Nest] 27380 - 06/19/2021, 4:29:59 PM [ExceptionsHandler] notNull Violation: User.name cannot be null,
notNull Violation: User.email cannot be null,
notNull Violation: User.password cannot be null,
notNull Violation: User.gender cannot be null +10917ms
SequelizeValidationError: notNull Violation: User.name cannot be null,
notNull Violation: User.email cannot be null,
notNull Violation: User.password cannot be null,
notNull Violation: User.gender cannot be null
at InstanceValidator._validate (D:\code\nodejs\blog-api\node_modules\sequelize\lib\instance-validator.js:78:13)