Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongoose模型静态/方法不包含“中的值”;这";_Javascript_Node.js_Mongodb_Mongoose_Passport.js - Fatal编程技术网

Javascript Mongoose模型静态/方法不包含“中的值”;这";

Javascript Mongoose模型静态/方法不包含“中的值”;这";,javascript,node.js,mongodb,mongoose,passport.js,Javascript,Node.js,Mongodb,Mongoose,Passport.js,我试图用mongodb用户模型实现passport js身份验证。现在您可以看到,我已经在用户模型上创建了一个方法。因此,当我在用户实例上应用此方法时,我希望“this”保存所有用户对象。但这种情况并没有发生。下面是一个工作代码,但我已经传递了一个额外的变量使其工作。但我不想那样做。我在哪里犯错 下面是signin的passport配置文件 var passport = require('passport'); var User = require('../models/users'); var

我试图用mongodb用户模型实现passport js身份验证。现在您可以看到,我已经在用户模型上创建了一个方法。因此,当我在用户实例上应用此方法时,我希望“this”保存所有用户对象。但这种情况并没有发生。下面是一个工作代码,但我已经传递了一个额外的变量使其工作。但我不想那样做。我在哪里犯错

下面是signin的passport配置文件

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);
与函数表达式相比,箭头函数表达式的语法更短,并且不绑定自己的this、arguments、super或new.target。箭头函数总是匿名的。这些函数表达式最适合于非方法函数,它们不能用作构造函数。


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
/*********************************************************/
}
----------

箭头函数–也称为“胖箭头”函数,来自CoffeeScript (一种翻译语言)是一种更简洁的写作语法 函数表达式。他们使用一个新的令牌=>,看起来像一个 宽箭头。Arrow函数是匿名的,可以更改其绑定方式 在功能上

箭头函数使我们的代码更加简洁,并简化了函数 作用域和关键字this。它们是一行微型函数 在其他语言(如C#或Python)中的工作方式与Lambdas非常相似。(另见 lambdas(在JavaScript中)。通过使用箭头函数,我们避免了 键入函数关键字return关键字(在箭头中是隐式的 函数)和花括号


使用Mongoose Schema.methods或Schema.statics时不要使用箭头函数,因为箭头函数不会像函数表达式那样绑定this关键字。因此,基本上,当您尝试使用箭头函数作为Schema.method()的参数时,每次引用模式中的定义值时,您都会得到未定义的值。

尝试
函数(密码,x){
而不是
(密码,x)=>
Hi@GrégoryNEUT,谢谢你的帮助。事情进展得很好。但我的问题是
console.log(this)
上面的一行返回了一个null。但这应该返回一个用户对象。至少我希望它能这样做:-(问题是当你使用箭头函数时(ES语法),函数中的上下文是继承的。Idk,即使使用常规上下文,您也可以从这里访问用户,但您可以尝试。哦…它起了作用。我不知道=>和函数之间的细微差别…需要阅读它们。感谢您的指导。我将它作为答案k来结束您的问题。