express.js和mysql身份验证

时间:2016-10-20 08:36:18

标签: javascript mysql node.js forms express

嘿伙计们我一直在尝试使用express.jsmysql创建一个注册表单,但是我的路由没有工作,因为他们假设它正在跳转到else语句,即使所有的正确填写的详细信息,这是我的代码。

User.js内容:

exports.register = function(req, res, next) {
    req.getConnection(function(err, connection) {
        if (req.body.name && req.body.email && req.body.password && req.body.confirmPassword) {

        // if user puts non matching passwords
        if (req.body.password !== req.body.confirmPassword) {
            var err = new Error('passwords do not match ')
            err.status = 400;
            return next(err)
        } 
       // if user do not put name and email on registration
        if (!req.body.name && !req.body.email) {
            var err = new Error('Both name and email need to be inputed');
            err.status = 400;
            return next(err)
        }

       // if user puts correct info the get save to database
        var newUser = {
            name: req.body.name,
            email: req.body.email,
            password: req.body.password,
            confirmPassword: req.body.confirmPassword
        };

        connection.query('insert into Users set ?', newUser, function(err, results) {
            if (err) return next(err);
            req.flash("success", 'Welcome', newUser.name);
            req.session.user = newUser;
            res.redirect('/categories');
        });

    } else {
        var err = new Error('All fields must be filled. ')
        err.status = 400;
        return next(err)
    }
});
}

有人可以帮我确定我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

从客户端发送适当的值。

  

并确保将body-parser代码放在服务器主文件中的所有路径之上。

var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));