3
オブジェクトがmongodbに保存される前にパスワードをハッシュするために、私はmongooseを使って組み込みのプリセーブフックを使用します。 しかし、更新中にハッシュを処理する正しい方法は何ですか?mongooseの更新時にパスワードをハッシュ
私はこれを更新前のフックで解決しようとしましたが、これはモデルの検証(パスワードの長さなど)をバイパスするため、いくつかの重大な欠点があります。
私のアプローチでした(短縮):だから
userSchema.pre('findOneAndUpdate', function (next) {
let query = this;
let update = query.getUpdate();
if (!update.password) {
return next();
}
hashPassword(update.password, function (err, hash) {
//handle error or go on...
//e.g update.password = hash;
});
});
、この問題を解決するための好ましい方法は何ですか?
私は前'save'
ミドルウェア使用することになり