node.js - object object error in signup with passport in express 对象错误

标签 node.js express passport.js

我在 NodeJs 项目中使用 Passport 和 Express。 我有一个包含字段的用户模型:id、密码和电子邮件。当我尝试注册时,它抛出了这个错误:

[object Object]

进入表单,它不会在数据库中发布用户数据。在控制台中,它显示

POST/signup 302 -58.

这是整个 passport.js 文件:

var LocalStrategy    = require('passport-local').Strategy;

// load up the user model
var configDB = require('./database.js');
var Sequelize = require('sequelize');
var sequelize = new Sequelize(configDB.url);

var User       = sequelize.import('../app/models/users');
User.sync();

// load the auth variables
var configAuth = require('./auth'); // use this one for testing

module.exports = function(passport) {

    // used to serialize the user for the session
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

    // used to deserialize the user
    passport.deserializeUser(function(id, done) {
        User.findById(id).then(function(user){
            done(null, user);
        }).catch(function(e){
            done(e, false);
        });
        });
=========================================================================
        // LOCAL LOGIN =============================================================
    passport.use('local-login', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
    },
    function(req, email, password, done) {      
            User.findOne({ where: { email: email }})
                .then(function(user) {
                    if (!user) {
                        done(null, false, req.flash('loginMessage', 'Unknown user'));
                    } else if (!user.validPassword(password)) {
                        done(null, false, req.flash('loginMessage', 'Wrong password'));
                    } else {
                        done(null, user);
                    }
                })
                .catch(function(e) { 
                    done(null, false, req.flash('loginMessage',e.name + " " + e.message));
                });             
    }));

    =========================================================================
    // LOCAL SIGNUP ============================================================
    passport.use('local-signup', new LocalStrategy({
        // by default, local strategy uses username and password, we will override with email
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not)
    },
    function(req, email, password, done) {        
        //  Whether we're signing up or connecting an account, we'll need
        //  to know if the email address is in use.

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

                // check to see if there's already a user with that email
                if (existingUser) 
                    return done(null, false, req.flash('error', 'That email is already taken.'));

                //  If we're logged in, we're connecting a new local account.
                if(req.user) {
                    var user            = req.user;
                    user.email    = email;
                    user.password = User.generateHash(password);
                    user.save().catch(function (err) {
                        throw err;
                    }).then (function() {
                        done(null, user);
                    });
                } 
                //  We're not logged in, so we're creating a brand new user.
                else {
                    // create the user
                    var newUser = User.build ({email: email, password: User.generateHash(password)}); 
                    newUser.save().then(function() {done (null, newUser);}).catch(function(err) { done(null, false, req.flash('error', err));});
                }
            })
            .catch(function (e) {
                done(null, false, req.flash('loginMessage',e.name + " " + e.message));              
            })

    }));

在 routes.js 中

// locally --------------------------------
    // LOGIN ===============================
    // show the login form
    app.get('/login', function(req, res) {
        res.render('login.ejs', { message: req.flash('loginMessage') });
    });

    // process the login form
    app.post('/login', passport.authenticate('local-login', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/login', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

    // SIGNUP =================================
    // show the signup form
    app.get('/signup', function(req, res) {
        res.render('signup.ejs', { message: req.flash('loginMessage') });
    });

    // process the signup form
    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect : '/profile', // redirect to the secure profile section
        failureRedirect : '/signup', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));

提前致谢。

最佳答案

我找到了解决方案!!!比想的简单,粗心而已。 在用户模型中,我定义了密码字段:

password: {
      type: DataTypes.STRING(25),
      allowNull: true,
      defaultValue: ''
    },

而且加密后,这个字段长度太小,无法存储值。所以我改成了255。

关于node.js - object object error in signup with passport in express 对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43666926/

相关文章:

javascript - 为已登录的用户手动创建 Passport session

javascript - 跨 nodejs 应用程序的多个实例共享相同的 session

PHP + Node - 将参数传递给已经运行的进程

html - 我来自 html 的 POST 没有返回任何值,nodejs,express

javascript - 通过 Express 中间件将 req.body 传递给路由

node.js - 尝试使用访问 token 访问 Spotify Web api 时收到 401

python - OpenShift 每小时 CRON 每 12 小时关闭一次

mysql - Node.js mySQL 无法创建连接

javascript - 同步 dnode 和 Express 身份验证 - 如何?

node.js - 使用 Mongoose、Express 删除用户