I have been trying to get the Error Message after I have used ZOD for creating Custom Errors. But unfortunately getting the TypeError as the “.errors” is empty (as it says in the terminal).
Kindly help me with the solution as I am just new to learning MERN.
Error is generated in my validate-middleware.js file –
const validate = (schema) => async (req, res, next) => {
try {
const parseBody = await schema.parseAsync(req.body)
req.body = parseBody
next()
} catch(e) {
const status = 422
const errorMessage = "Input is not given properly"
const details = e.errors[0].messsage
console.log(details)
const error = {status, errorMessage, details}
next(error)
}
}
module.exports = validate
Error in Terminal –
D:2024MERNservermiddlewarevalidate-middleware.js:9
const details = e.errors[0].messsage
^
TypeError: Cannot read properties of undefined (reading '0')
at D:2024MERNservermiddlewarevalidate-middleware.js:9:33
at Layer.handle [as handle_request] (D:2024MERNservernode_modulesexpresslibrouterlayer.js:95:5)
at next (D:2024MERNservernode_modulesexpresslibrouterroute.js:149:13)
at Route.dispatch (D:2024MERNservernode_modulesexpresslibrouterroute.js:119:3)
at Layer.handle [as handle_request] (D:2024MERNservernode_modulesexpresslibrouterlayer.js:95:5)
at D:2024MERNservernode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (D:2024MERNservernode_modulesexpresslibrouterindex.js:346:12)
at next (D:2024MERNservernode_modulesexpresslibrouterindex.js:280:10)
at Function.handle (D:2024MERNservernode_modulesexpresslibrouterindex.js:175:3)
at router (D:2024MERNservernode_modulesexpresslibrouterindex.js:47:12)
My Zod validation file – auth-validator.js
const loginValidation = z.object({
email: z
.string({required_error: "Email cannot be empty during Login"})
.trim(),
password: z
.string({required_error: "Password cannot be empty during Login"})
}
)
New contributor
user9747920 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.