so i’m working with Joi for validation, and i’ve encountered this error when trying to post with postman.
i’m follwing a tutorial, i tried to write it differently, but still have the same issue.
i’m trying to access the error message. ( first selecting the error, then the details, then the message )
in the tutorial, it looks like this
res.send(error.details[0].message)
i can see the error, but once i select details, the response is empty.
let me know if you need anything else.
Thank you in advance.
The Lord be with you and save you all, your families and friends 🙂
Advertisement
Answer
It seems like error
does not have a property called details
. That’s why error.details
is undefined. Thus, when trying to access the element of the first index of the value undefined
you’ll get an error.
To fix:
- Make sure the error object contains a property details of type Array
- If error.details will depend on other code blocks (sometimes it is defined, sometimes it isn’t), you can add a ternary expression to tell your code what to in case
error.details
is indeed undefined. Example:
err.details ? res.send(error.details[0].message) : res.send("error")
Which translates to
if (err.details) { // if defined res.send(error.details[0].message) // Send the message from the error } else { res.send("error") // Send a general message }