I have a simple schema Blogs:
canst blogSchema = new mongoose.Schema({
title: String,
author: String,
url: String,
likes: Number
})
and I'm trying to make my API return a 400 error code whenever non String is passed to fields and I'm catching it with this bit of code in my API:
try {
new_blog = new Blog({
title: request_body.title,
author: request_body.author,
url: request_body.url,
likes: request_body.likes || 0
})
} catch (error_message) {
const error_body = {}
error_body.error = error_message.message
return response.status(400).send(error_body)
}
However I'm getting the following error:
ValidationError: Blog validation failed: title: Cast to string failed for value "[ 111 ]" (type Array) at path "title"
despite me having try/catch. What am I doing wrong here and how can I fix this behavior?