I am working with mongoose and axios in node.js/react and need to obtain the specific validation error in the client side so that i can update the form. In this case the user email is already the database and not unique. the error returned to the front end appears useless. i tried err.message on the front end but it gives me nothing. How do i call the specific error message telling me the actual error?
FRONTEND API IS CALLED AND INCLUDES:
API.createUser({
userEmail: this.state.profile.email.toLowerCase(),
})
.catch(err => console.log(err))
BACKEND CONTROLLER
db.WmUser
.create(req.body)
.then(dbModel => res.json(dbModel))
.catch(err => res.status(422).json(err));
MONGOOSE SCHEMA INCLUDES:
userEmail: {type: String, unique: true, required: true},
RESULT RETURNS FRONT END ERROR:
Error: Request failed with status code 422
at createError (createError.js:16)
at settle (settle.js:18)
at XMLHttpRequest.handleLoad (xhr.js:77)
RESULT RETURNS IN THE BACKEND IN CONTROLLER:
if i use the following code in the controller i get the error message i need but don't know how to bring it back to the front end:
.catch(err=> console.log(err.message))