如何获取schema.pre()方法中的特定字段?
我用mongoose使用nodejs,我想在更新文档之前获取用户密码。 我正在使用schema.pre()
方法在更新之前被调用,但是我不知道如何在这个方法中获得用户密码。
这里是我的UserSchema.pre()
方法:
UserSchema.pre('update', function(next){ var user = this; next(); });
和我的模式:
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 } }] });
希望你能帮助我,谢谢。
UserSchema.pre('update', function(next){ var user = this; console.log(user._update); // contains the fields to be be updated next(); });
当你更新单个用户时,我build议使用预先findOneAndUpdate
钩子。