I'm trying to implement some validation in a sign up system, but I get the error:
TypeError: req.checkBody is not a function
from the following code:
module.exports = function(app, express) {
var express = require('express');
var api = express.Router();
// post users to database
api.post('/signup', function(req, res) {
var email = req.body.email;
var password = req.body.password;
var password2 = req.body.password2;
var key = req.body.key;
// Validation
req.checkBody('email', 'Email is required.').notEmpty();
req.checkBody('email', 'Email is not valid').isEmail();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
var errors = req.validationErrors();
if(errors) {
res.render('register', {
errors: errors
});
} else {
var user = new User({
email: email,
password: password
});
var token = createToken(user);
}
// save to database
user.save(function(err) {
if (err) {
res.send(err);
return;
}
res.json({
success: true,
message: 'User has been created',
token: token
});
});
});
I've checked and it's getting the info from the front end, and I've had almost the same code work in another app (where is wasn't wrapped in module.exports = function(app, express) { }
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…