app.use(validator()) is not a function

var router = express.Router();
const { check, validationResult } = require('express-validator');

router.post('/register',
  [
    check('email', 'Email is not valid').isEmail(),
    check('username', 'Username field is required').not().isEmpty(),
    check('password', 'Password field is required').not().isEmpty())
  ], 
  function(req, res, next) {

  // Check Errors
  const errors = validationResult(req);
  if (errors) {
    console.log(errors);
    res.render('register', { errors: errors.array() });
  }
  else {
    console.log('No Errors');
    res.render('dashboard', { message: 'Successful Registration.' });
  }
});
// working express validator
// use a different method to Handle errors

3.75
8
Phoenix Logan 186120 points

                                    console.log(`error: ${err}`);
res.status(code).json({
    error: err
});
// good error handling by express validation Pg 2
// make utils/app.js and write this code inside it

3.75 (8 Votes)
0
3.6
5
Phoenix Logan 186120 points

                                    route.post([check('nome', 'O nome é obrigatório').not().isEmpty(),
            check('email', 'O email é obrigatório').isEmail(),
            check('password', 'O password é obrigatório').not().isEmpty(),], 

(req, res)=>{

    const errors = validationResult(req);

    if(!errors.isEmpty()){

        app.utils.error.send(errors, req, res);
        return false;
    }

    db.insert(req.body, (err, user)=>{ //entering data into database

        if(err){
            app.utils.error.send(err, req, res);
        }else{
            res.status(200).json(user);
            
        }
    });
// Good Error Handling in express validator Pg 1 

3.6 (5 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source