typescript - Typeorm 改变传递给 create 方法的值,避免这种情况的最佳方法是什么?
问题描述
从下面的代码看来,TypeORM 改变了传递给 create 方法的值(下面的代码)。将值传递到 create 方法而不改变它的最佳/最有效的解决方法是什么?是 JSON 字符串化然后 JSON 解析,还是操作太昂贵?
这种行为以前从来不是问题,但在我测试组件时出现了。我写了一个测试,期望 createUserDto 不等于返回的用户实体,但测试失败,因为它们是相同的。
async createUser(createUserDto: CreateUserDto) => {
console.log(createUserDto) // { email: 'joe', password: '123456' }
const user = await userRepository.create(createUserDto)
const salt = ....
const hash = ....
user.password = hash
console.log(createUserDto) // { email: 'joe', password: 'hashed pw' }
console.log(user) // { email: 'joe', password: 'hashed pw' }
Object.is(createUserDto, user) // true
return await userRepository.save(user)
}