Mongoose 模型 static/method 未持有 "this" 中的值
Mongoose Model static/method not holding the value in "this"
我正在尝试使用 mongodb 用户模型实现护照 js 身份验证。现在如您所见,我已经在用户模型上创建了一个方法。因此,当我将此方法应用于用户实例时,我希望 "this" 包含所有用户对象。但在这种情况下并没有发生这种情况。以下是一个工作代码,但我已经传递了一个附加变量以使其工作。但我不想那样做。我哪里出错了?
下面是用于登录的护照配置文件
var passport = require('passport');
var User = require('../models/users');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser((user, done)=>{
done(null, user.id);
});
passport.deserializeUser((id, done)=>{
User.findById(id, (err, user)=>{
done(err, user);
});
});
passport.use('local.signin', new LocalStrategy({
usernameField: 'email', passwordField: 'password', passReqToCallback: true
},(req, email, password, done) => {
User.findOne({email:email}, (err, user) => {
if (err){ return done(err)}
if (!user){return done(null, false, {message:'This email is not registered'})}
if (!user.validatePassword(password, user.password)){
/**********************************************/
//is this field user.password really necessary?
/**********************************************/
return done(null, false, {message: 'Authentication Failed'})
} else {
return done(null, user);
}
});
}));
用户模型如下:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var userSchema = new Schema({
salutation: {type: String, required: false},
firstname: {type: String, required: true},
lastname: {type: String, required: false},
email: {type: String, required: true},
password: {type: String, required: true}
});
userSchema.methods.validatePassword = (password, x) => {
console.log(this); //this is returning null
return bcrypt.compareSync(password, x);
/*********************************************************/
//I was excepting this.password to work instead of using x
/*********************************************************/
}
userSchema.methods.myCourses = (userId) => {
console.log(this.enrolledFor);
}
module.exports = mongoose.model('User', userSchema);
An arrow function expression has a shorter syntax compared to function expressions and does not bind its own this, arguments, super, or new.target. Arrow functions are always anonymous. These function expressions are best suited for non-method functions and they can not be used as constructors.
developer.mozilla
ECMA2015 标准也称为 ES6 允许使用箭头函数,这些函数从上层上下文继承它们的上下文。
解决方法是使用常规函数语法。
userSchema.methods.validatePassword = function (password, x) {
console.log(this); //this is returning null
return bcrypt.compareSync(password, x);
/*********************************************************/
//I was excepting this.password to work instead of using x
/*********************************************************/
}
----------
Arrow functions – also called “fat arrow” functions, from CoffeeScript
(a transcompiled language) are a more concise syntax for writing
function expressions. They utilize a new token, =>, that looks like a
fat arrow. Arrow functions are anonymous and change the way this binds
in functions.
Arrow functions make our code more concise, and simplify function
scoping and the this keyword. They are one-line mini functions which
work much like Lambdas in other languages like C# or Python. (See also
lambdas in JavaScript). By using arrow function we avoid having to
type the function keyword, return keyword (it’s implicit in arrow
functions), and curly brackets.
在使用 Mongoose Schema.methods 或 Schema.statics 时不要使用箭头函数,因为箭头函数不像函数表达式那样绑定 this 关键字。所以基本上,当你尝试使用箭头函数作为 Schema.method() 的参数时,每次你从你的模式中引用一个定义值时你都会得到未定义的。
我正在尝试使用 mongodb 用户模型实现护照 js 身份验证。现在如您所见,我已经在用户模型上创建了一个方法。因此,当我将此方法应用于用户实例时,我希望 "this" 包含所有用户对象。但在这种情况下并没有发生这种情况。以下是一个工作代码,但我已经传递了一个附加变量以使其工作。但我不想那样做。我哪里出错了?
下面是用于登录的护照配置文件
var passport = require('passport');
var User = require('../models/users');
var LocalStrategy = require('passport-local').Strategy;
passport.serializeUser((user, done)=>{
done(null, user.id);
});
passport.deserializeUser((id, done)=>{
User.findById(id, (err, user)=>{
done(err, user);
});
});
passport.use('local.signin', new LocalStrategy({
usernameField: 'email', passwordField: 'password', passReqToCallback: true
},(req, email, password, done) => {
User.findOne({email:email}, (err, user) => {
if (err){ return done(err)}
if (!user){return done(null, false, {message:'This email is not registered'})}
if (!user.validatePassword(password, user.password)){
/**********************************************/
//is this field user.password really necessary?
/**********************************************/
return done(null, false, {message: 'Authentication Failed'})
} else {
return done(null, user);
}
});
}));
用户模型如下:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');
var userSchema = new Schema({
salutation: {type: String, required: false},
firstname: {type: String, required: true},
lastname: {type: String, required: false},
email: {type: String, required: true},
password: {type: String, required: true}
});
userSchema.methods.validatePassword = (password, x) => {
console.log(this); //this is returning null
return bcrypt.compareSync(password, x);
/*********************************************************/
//I was excepting this.password to work instead of using x
/*********************************************************/
}
userSchema.methods.myCourses = (userId) => {
console.log(this.enrolledFor);
}
module.exports = mongoose.model('User', userSchema);
An arrow function expression has a shorter syntax compared to function expressions and does not bind its own this, arguments, super, or new.target. Arrow functions are always anonymous. These function expressions are best suited for non-method functions and they can not be used as constructors. developer.mozilla
ECMA2015 标准也称为 ES6 允许使用箭头函数,这些函数从上层上下文继承它们的上下文。
解决方法是使用常规函数语法。
userSchema.methods.validatePassword = function (password, x) {
console.log(this); //this is returning null
return bcrypt.compareSync(password, x);
/*********************************************************/
//I was excepting this.password to work instead of using x
/*********************************************************/
}
----------
Arrow functions – also called “fat arrow” functions, from CoffeeScript (a transcompiled language) are a more concise syntax for writing function expressions. They utilize a new token, =>, that looks like a fat arrow. Arrow functions are anonymous and change the way this binds in functions.
Arrow functions make our code more concise, and simplify function scoping and the this keyword. They are one-line mini functions which work much like Lambdas in other languages like C# or Python. (See also lambdas in JavaScript). By using arrow function we avoid having to type the function keyword, return keyword (it’s implicit in arrow functions), and curly brackets.
在使用 Mongoose Schema.methods 或 Schema.statics 时不要使用箭头函数,因为箭头函数不像函数表达式那样绑定 this 关键字。所以基本上,当你尝试使用箭头函数作为 Schema.method() 的参数时,每次你从你的模式中引用一个定义值时你都会得到未定义的。