TypeError: Cannot read property 'findOne' of undefined while using sequelize in node

TypeError: Cannot read property 'findOne' of undefined while using sequelize in node

我在尝试使用 passport-local 和 sequelize MySQL 进行用户身份验证时收到 belo 提到的错误。当 运行 服务器正在 SQL 中创建新的 table 时,如果尚未创建,但是当我点击注册按钮时,它会立即显示错误。

错误:

TypeError: Cannot read property 'findOne' of undefined
    at Strategy._verify (E:\Web Development\node-SQL-Sequelize-passport-local\config\passport\passport.js:19:13)
    at Strategy.authenticate (E:\Web Development\node-SQL-Sequelize-passport-local\node_modules\passport-local\lib\strategy.js:88:12)
    at attempt (E:\Web Development\node-SQL-Sequelize-passport-local\node_modules\passport\lib\middleware\authenticate.js:361:16)
    at authenticate (E:\Web Development\node-SQL-Sequelize-passport-local\node_modules\passport\lib\middleware\authenticate.js:362:7)

我的server.js长得像:

app.use(passport.initialize());

app.use(passport.session()); 




//Models
var models = require("./app/models");

//Routes
var authRoute = require('./app/routes/auth.js')(app,passport);
require('./config/passport/passport.js')(passport, models.user);

//Sync Database
models.sequelize.sync().then(function() {

    console.log('Nice! Database looks fine')

}).catch(function(err) {

    console.log(err, "Something went wrong with the Database Update!")

});

app.get('/', function(req, res) {

    res.send('Welcome to Passport with Sequelize');

});

我的user.js文件:

module.exports = function(sequelize, Sequelize) {

    var User = sequelize.define('userInfo', {

        id: {
            autoIncrement: true,
            primaryKey: true,
            type: Sequelize.INTEGER
        },

        firstname: {
            type: Sequelize.STRING,
            notEmpty: true
        },

        lastname: {
            type: Sequelize.STRING,
            notEmpty: true
        },

        username: {
            type: Sequelize.TEXT
        },

        about: {
            type: Sequelize.TEXT
        },

        email: {
            type: Sequelize.STRING,
            validate: {
                isEmail: true
            }
        },

        password: {
            type: Sequelize.STRING,
            allowNull: false
        },

        last_login: {
            type: Sequelize.DATE
        },

        status: {
            type: Sequelize.ENUM('active', 'inactive'),
            defaultValue: 'active'
        }


    });

    return User;

}

我的passport.js文件:

var bCrypt = require('bcrypt-nodejs');

module.exports = function(passport, user) {
    var User = user;
    var LocalStrategy = require('passport-local').Strategy;
    passport.use('local-signup', new LocalStrategy(

    {
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true // allows us to pass back the entire request to the callback

    },function(req, email, password, done) {
        var generateHash = function(password) {

        return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);

        };
        User.findOne({
        where: {
            email: email
        }
        }).then(function(user) {

            if (user)

            {

                return done(null, false, {
                    message: 'That email is already taken'
                });

            } else

            {

                var userPassword = generateHash(password);

                var data =

                    {
                        email: email,

                        password: userPassword,

                        firstname: req.body.firstname,

                        lastname: req.body.lastname

                    };


                    User.create(data).then(function(newUser, created) {

                    if (!newUser) {

                        return done(null, false);

                    }

                        if (newUser) {

                            return done(null, newUser);

                        }

                    });

            }

        });

    }

));

}

查看此示例:github.com/sequelize/express-example。特别是 models/index.js 用于模型加载。

那个例子中处理的方式是:

require('./config/passport/passport.js')(passport, models.userInfo);

因为"userInfo"在你的模型中定义user.js: