Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
Why can't I catch mongoose ValidationError?

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!