尝试在使用@nestjs/mongoose保存之前加密密码。阅读一些示例,它们类似于下面的伪代码:
UsersSchema.pre('save', (next: any) => {
if (!this.isModified('password')) return next();
this.password = encrypt(this.password, 10);
next();
});这是没有类型的,所以我console.log获取信息,它似乎是一个完全不同的对象。
// user.entity.ts
const UsersSchema = SchemaFactory.createForClass(Users);
UsersSchema.pre('save', () => {
console.log(this);
});
export { UsersSchema };日志结果:
{
Users: [class Users],
UsersSchema: Schema {
obj: {
username: [Object],
name: [Object],
email: [Object],
password: [Object]
},
paths: {
username: [SchemaString],
name: [SchemaString],
email: [SchemaString],
password: [SchemaString],
_id: [ObjectId],
__v: [SchemaNumber]
},
aliases: {},
subpaths: {},
...and more不知道这是怎么回事,我在医生那里也找不到这个。也许我做错了?
发布于 2022-07-21 13:27:49
尝试使用常规函数而不是箭头函数,这应该可以修复它!
UsersSchema.pre('save', function (next: any) {
if (!this.isModified('password')) return next();
this.password = encrypt(this.password, 10);
next();
});https://stackoverflow.com/questions/72900982
复制相似问题