Seeking to encrypt passwords before saving using @nestjs/mongoose. Came across examples written in pseudocode like this:
UsersSchema.pre('save', (next: any) => {
if (!this.isModified('password')) return next();
this.password = encrypt(this.password, 10);
next();
});
However, there doesn't seem to be a specific type assigned to 'this', so I logged it to see more information, and it turns out to be a different object altogether.
// user.entity.ts
const UsersSchema = SchemaFactory.createForClass(Users);
UsersSchema.pre('save', () => {
console.log(this);
});
export { UsersSchema };
Here's the log result:
{
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
I'm unsure about how this behavior works as it wasn't found in the documentation. Could use some guidance on whether I'm approaching this correctly?