简体   繁体   中英

express.js and mysql authentication

Hey guys I've been trying to create a signup form using express.js and mysql but my routes are not working as they are suppose to instead it is jumping to the else statement even with all the details filled in correctly, here is my code.

User.js content:

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)
    }
});
}

Can someone help me identify where I'm going wrong?

Send your proper values from client side.

And make sure to put your body-parser code above all the routes in your server main file.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM