2017-08-10 19 views
1

私はmongooseでnodejsを使用しています。文書を更新する前にユーザーのパスワードを取得したいと思います。更新する前にschema.pre()メソッドを呼び出すのですが、このメソッドでユーザーパスワードを取得する方法はわかりません。schema.pre()メソッドで特定のフィールドを取得する方法は?

UserSchema.pre('update', function(next){ 
    var user = this; 
    next(); 
}); 

と私のスキーマ:

はここに私のUserSchema.pre()方法である

var UserSchema = new mongoose.Schema({ 

email: { 
    type: String, 
    required: true, 
    minlength: 1, 
    trim: true, 
    unique: true 
}, 
password: { 
    type: String, 
    required: true, 
    minlength: 6 
}, 
firstName: { 
    type: String, 
    required: true, 
    minlength: 1, 
    trim: true 
}, 
lastName: { 
    type: String, 
    required: true, 
    minlength: 1, 
    trim: true 
}, 
tokens: [{ 
    access:{ 
     type: String, 
     required: true 
    }, 
    token:{ 
     type: String, 
     required: true 
    } 
}] 
}); 

あなたはこれで私を助けることを願って、 ありがとうございました。

+0

投稿にスキーマの定義を追加してください。 –

答えて

0
UserSchema.pre('update', function(next){ 
    var user = this; 
    console.log(user._update); // contains the fields to be be updated 
    next(); 
}); 

シングルユーザーを更新しているので、代わりにfindOneAndUpdateフックを使用することをおすすめします。

関連する問題