I have dilemma, trying to add some pre-logic to a mongoose model using pre
middleware and can not access the this
instance as usual.
UserSchema.pre('save', next => {
console.log(this); // logs out empty object {}
let hash = crypto.createHash('sha256');
let password = this.password;
console.log("Hashing password, " + password);
hash.update(password);
this.password = hash.digest('hex');
next();
});
Question: *Is there a way to access the this
instance?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…