1var router = express.Router();
2const { check, validationResult } = require('express-validator');
3
4router.post('/register',
5 [
6 check('email', 'Email is not valid').isEmail(),
7 check('username', 'Username field is required').not().isEmpty(),
8 check('password', 'Password field is required').not().isEmpty())
9 ],
10 function(req, res, next) {
11
12 // Check Errors
13 const errors = validationResult(req);
14 if (errors) {
15 console.log(errors);
16 res.render('register', { errors: errors.array() });
17 }
18 else {
19 console.log('No Errors');
20 res.render('dashboard', { message: 'Successful Registration.' });
21 }
22});
23// working express validator
24// use a different method to Handle errors
1route.post([check('nome', 'O nome é obrigatório').not().isEmpty(),
2 check('email', 'O email é obrigatório').isEmail(),
3 check('password', 'O password é obrigatório').not().isEmpty(),],
4
5(req, res)=>{
6
7 const errors = validationResult(req);
8
9 if(!errors.isEmpty()){
10
11 app.utils.error.send(errors, req, res);
12 return false;
13 }
14
15 db.insert(req.body, (err, user)=>{ //entering data into database
16
17 if(err){
18 app.utils.error.send(err, req, res);
19 }else{
20 res.status(200).json(user);
21
22 }
23 });
24// Good Error Handling in express validator Pg 1