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

标签 node.js reactjs express passport.js

我正在尝试在我的网络应用程序上使用 Passport 身份验证。我正在使用 Sequelize ORM、Reactjs 前端以及 Express 和 Node 后端。现在,当我注册用户时,一切正常。当我尝试登录时出现问题。我看到用户查询数据库以查找具有正确电子邮件的用户,但是当需要比较密码时,我发现了一个错误。

“未处理的拒绝类型错误: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;
}

我还附上了错误的图像。 Error Message

最佳答案

从sequelize版本>4开始,它改变了实例方法的定义方式。

他们现在遵循更加基于类的方法, 来自 Docs 的示例了解如何完成

 const Model = sequelize.define('Model', { ... });

 // Class Method 
Model.associate = function (models) { ...associate the models }; 
// Instance Method
Model.prototype.someMethod = function () {..}

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

关于node.js - 未处理的拒绝 TypeError : dbUser. validPassword 不是函数 Sequelize Node React Passport,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44909738/

相关文章:

java - 在 Node 中读取 Java 生成的公钥

javascript - for 循环后数组未填充?

javascript - react 拆分面板调整大小

node.js - 无法在NodeJS中解决: 'TypeError: Converting circular structure to JSON'

node.js - 将前端 native websocket 客户端连接到 Node.js Socket.io 服务器

node.js - 一个项目中的快速 session 和代币

node.js - Apollo 服务器 : How can I add a custom endpoint for Stripe webhook?

node.js - react native 音频流

html - React.js 中的图片和文本 slider 响应问题

node.js - fs.createWriteStream 错误 :ENOENT