Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
I can't make signup post request in MERN stack project

my signup function was working fine. but it doesn't now. I take bad request 400 error. when I change the error code to 401 or 500 in validator, I take that error this time. and it is sending me all the validation errors on postman. what should I do? it prints {username: 'cem1', email: 'cem1@hotmail.com', password: '123456'} by console.log(user) in clickSubmitSignup.

{ "error": [ "username is required", "email must contain @", "Email must be between 3 to 32 characters", "password is required", "password must contain at least 6 characters", "password must contain a number" ] } this is console output xhr.js:220 POST localhost:8080/signup 400 (Bad Request) –

const clickSubmitSignup = event => {
    event.preventDefault()
    const user = {
        username,
        email,
        password
    };
    console.log(user)
    
   axios.post('http://localhost:8080/signup', user)
    .then(data=> {
        if(data.error){
            setError(data.error)
        }else{
             setError("")
             setUsername("")
             setEmail("")
             setPassword("")
             setOpen(true)
        }
    })
    .catch(error=> {setError(error.message)})
};    

exports.userSignupValidator = (req, res, next)=>{
   req.check('username', 'username is required').notEmpty()
   req.check('email', 'Email must be between 3 to 32 characters')
   .matches(/.+\@.+\..+/)
   .withMessage('email must contain @')
   .isLength({
       min:4,
       max: 200
  })
  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)
      return res.status(400).json({error: firstError})
   }
   next()

}

exports.signup = async(req, res)=>{
const emailExists = await User.findOne({email: req.body.email})
if(emailExists) return res.status(403).json({
    error: "email is taken"
}) 
const userNameExists = await User.findOne({username: req.body.username})
if(userNameExists) return res.status(403).json({
    error: "username is taken"
})
const user = await new User(req.body)
console.log(user)
await user.save()
res.status(200).json({message : "signup success"})
console.log(req.body)

}

router.post('/signup', userSignupValidator, signup)
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!