0
ユーザーのパスワードを暗号化するためにユーザーモデルのインスタンスを取得するためのフックを追加しましたが、モデルのインスタンスを取得できません。種類モデルのインスタンスを取得中にエラーが発生しました
はTypeErrorようなタイプのエラーを取得し、この私のコード
regUsers.pre('save',(next)=>{
// get access to this user model
const user = this;
// generate salt the run call back
bcrypt.genSalt(10,(err,salt)=>{
if(err){ return next(err);}
// hash(encrypt) our password using salt
bcrypt.hash(user.password,salt,null,(err,hash)=>{
if(err){ return next(err);}
//overwrite plain text password with encrypted password
user.password = hash;
next();
});
})
});
イム:未定義のプロパティ 'password' を読み込めません。 このerorrを取得している理由は何ですか?これを克服する方法は?
'this'は' mongoose.Query'のインスタンスを参照します.. –
@SunilBNこの場合、 'document middleware'が使用されています。' this'は、更新されるドキュメントへの参照であり、クエリではありません。ここで説明します:http://mongoosejs.com/docs/middleware.html –
[Mongoose pre/post midlewareの複製は、ES6を使用して\ [this \]インスタンスにアクセスできません](http://stackoverflow.com/questions)/36957440/mongoose-pre-post-midleware-cant-acces-this-instance-using-es6) –