0

I followed in the steps of a fullstack course. This problem is related to the express-validator dependency. I configured and pasted the code that was passed to me. And in the postman when sending in the sigup url, the following error message appeared:

{
         "error": "Email must contain @"
}

But that wasn’t supposed to happen, because I had inserted the @ in the email

{
"name": "dave",
"email": "[email protected]",
"password": "rrrrrr9"
}

Here are the information and codes for my application:

Express-validator version: ^ 5.3.1

app.js file:

const expressValidator = require ('express-validator')

app.use (expressValidator ())

validator/index.js file:

exports.userSignupValidator = (req, res, next) => {
  req.check ('name', 'Name is required'). notEmpty ()
  req.check ('name', 'Email must be between 3 to 32 characters')
     .matches (/.+\@.+\..+/)
     .withMessage ('Email must contain @')
     .isLength ({
       min: 4,
       max: 32
     })
     req.check ('password', 'Password is required'). notEmpty ()
     req.check ('password')
     .isLength ({min: 6})
     .withMessage ('Password must contain at least 6 characters')
     .matches (/ \ d /)
     .withMessage ("Password must contain a number")
       const errors = req.validationErrors ()
       if (errors) {
         const firstError = errors.map (error => error.msg) [0]
         return res.status (400) .json ({error: firstError})
       }
       next ()
}

routes/user.js file:

const express = require('express')
const router = express.Router()

const {userSignupValidator} = require('../validator')
router.post("/signup", userSignupValidator, signup)


module.exports = router

How can I solve it?

4
  • 3
    Shouldn't this req.check ('name', 'Email must be between 3 to 32 characters') be req.check ('email', 'Email must be between 3 to 32 characters')? Commented Feb 3, 2021 at 3:20
  • 1
    No problem. Does that solve your issue? Commented Feb 3, 2021 at 3:22
  • Yes! It does! Apparently I thought it had nothing to do with the problem, but it was exactly that little detail that was causing the error. Post this code of yours in the reply so I can upvote you Commented Feb 3, 2021 at 3:28
  • It's all right. Just give @Kalki222 that upvote. He got it right as well. Commented Feb 3, 2021 at 3:30

1 Answer 1

3

Try using this :-

     [
    check("name", "Name is required").not().isEmpty(),
    check("email", "Please enter valid email").isEmail(),
    check(
      "password",
      "Please enter valid password with 6 or more characters"
    ).isLength({ min: 6 }),
  ],
  async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(400).json({ errors: errors.array() });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.