Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda