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)