未处理的拒绝TypeError:dbUser.validPassword不是函数Sequelize Node React Passport

我正在尝试在我的web应用程序上使用Passport身份validation。 我使用Sequelize ORM,Reactjs前端和expression式和节点后端。 现在,当我注册一个用户一切正常。 当我尝试login时出现问题。 我看到用户查询数据库find用户正确的电子邮件,但是当是时候比较密码,我抓到一个错误。

“未处理的拒绝types错误:dbUser.validPassword不是函数”

这里是我的config / passport.js文件:

var passport = require("passport"); var LocalStrategy = require("passport-local").Strategy; var db = require("../models"); // Telling passport we want to use a Local Strategy. In other words, we want login with a username/email and password passport.use(new LocalStrategy( // Our user will sign in using an email, rather than a "username" { usernameField: "email" }, function(email, password, done) { // When a user tries to sign in this code runs db.User.findOne({ where: { email: email } }).then(function(dbUser) { // If there's no user with the given email if (!dbUser) { return done(null, false, { message: "Incorrect email." }); } // If there is a user with the given email, but the password the user gives us is incorrect else if (!dbUser.validPassword(password)) { return done(null, false, { message: "Incorrect password." }); } // If none of the above, return the user return done(null, dbUser); }); } )); // In order to help keep authentication state across HTTP requests, // Sequelize needs to serialize and deserialize the user // Just consider this part boilerplate needed to make it all work passport.serializeUser(function(user, cb) { cb(null, user); }); passport.deserializeUser(function(obj, cb) { cb(null, obj); }); // Exporting our configured passport module.exports = passport; 

这是我的用户模型:

 var bcrypt = require("bcrypt-nodejs"); [![enter image description here][1]][1]module.exports = function(sequelize, DataTypes){ var User = sequelize.define("User", { email: { type: DataTypes.STRING, allowNull: false, validate: { isEmail: true } }, password: { type: DataTypes.STRING, allowNull: false }, },{ classMethods: { associate: function(models) { User.hasOne(models.Educator, { onDelete: "cascade" }); User.hasOne(models.Expert, { onDelete: "cascade" }); } }, instanceMethods: { validPassword: function(password) { return bcrypt.compareSync(password, this.password); } }, // Hooks are automatic methods that run during various phases of the User Model lifecycle // In this case, before a User is created, we will automatically hash their password hooks: { beforeCreate: function(user, options) { console.log(user, options ) user.password = bcrypt.hashSync(user.password, bcrypt.genSaltSync(10), null); } } }) return User; } 

我还包括一个错误的形象。 错误信息

从集合版本大于4开始,它改变了实例方法的定义方式。

他们现在采用更多的基于class级的方法,来自Docs的样本是如何完成的

  const Model = sequelize.define('Model', { ... }); // Class Method Model.associate = function (models) { ...associate the models }; // Instance Method Model.prototype.someMethod = function () {..} 

您正在使用的语法对应于<4。