Mongoose预保存正在使用不正确的“这个”背景? [英] Mongoose pre save is using incorrect "this" context?
问题描述
从文档中可以看出这个这个
在Mongoose .pre('save')
方法应该是模型本身,但是在我的代码下面这个
一个空对象。
const Mongoose = require('./ lib / database')。
const Bcrypt = require('bcrypt');
const userSchema = new Mongoose.Schema({
email:{type:String,required:true,index:{unique:true}},
password:{type: String,required:true}
});
userSchema.pre('save',(next)=> {
const user = this;
Bcrypt.genSalt( ,salt)=> {
if(err){
return next(err);
}
Bcrypt.hash(user.password ,salt,(err,encrypted)=> {
if(err){
return next(err);
}
user。密码=加密;
next();
});
});
});
const User = Mongoose.model('User',userSchema);
保存用户时,我收到以下错误 [错误:数据和需要盐参数]
。
function createUser(email,password,next){
const user = new User({
email:email,
password:password
});
user.save((err)=> {
if(err){
return next(err);
}
return next(null,user);
});
}
createUser('test@email.com','testpassword',(err,user)=> {
if(err){
console.log(err);
}
else {
console.log(user);
}
process.exit() ;
});
如果我删除 .pre('save')
那么它当然可以节省罚款。我使用的Mongoose版本是4.2.6。
这里的问题是,rel =noreferrer>开发人员,开发人员,开发人员,开发人员,您必须使用简单的功能重写您的回调。这个小例子显示了diff
var obj = {};
obj.func1 = function(){
console.log(this === obj);
};
obj.func2 =()=> {
console.log(this === obj);
};
obj.func1(); // true
obj.func1.bind(obj)(); // true
obj.func2(); // false
obj.func2.bind(obj)(); // false
Can anyone figure out what's wrong with my code below?
From documentation it looks like the this
in Mongoose .pre('save')
method should be the model itself, but in my code below this
ends up being an empty object.
const Mongoose = require('./lib/database').Mongoose;
const Bcrypt = require('bcrypt');
const userSchema = new Mongoose.Schema({
email: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true }
});
userSchema.pre('save', (next) => {
const user = this;
Bcrypt.genSalt((err, salt) => {
if (err) {
return next(err);
}
Bcrypt.hash(user.password, salt, (err, encrypted) => {
if (err) {
return next(err);
}
user.password = encrypted;
next();
});
});
});
const User = Mongoose.model('User', userSchema);
When saving a user, I get the following error [Error: data and salt arguments required]
.
function createUser(email, password, next) {
const user = new User({
email: email,
password: password
});
user.save((err) => {
if (err) {
return next(err);
}
return next(null, user);
});
}
createUser('test@email.com', 'testpassword', (err, user) => {
if (err) {
console.log(err);
}
else {
console.log(user);
}
process.exit();
});
If I remove the .pre('save')
then it saves fine of course. Version of Mongoose I'm using is 4.2.6.
Problem here is a fat arrow functions. You must rewrite your callback with simple functions. Here small example to show the diff
var obj = {};
obj.func1 = function () {
console.log(this === obj);
};
obj.func2 = () => {
console.log(this === obj);
};
obj.func1(); // true
obj.func1.bind(obj)(); // true
obj.func2(); // false
obj.func2.bind(obj)(); // false
这篇关于Mongoose预保存正在使用不正确的“这个”背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!