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

93
Vistas
throw an error when $match doesn't find any matching doc

I have the following code:

const userSchema = new mongoose.Schema({
   name: String,
   loginAttempts: [ { IP: String } ]
})
const User = mongoose.model('User', userSchema, 'users')
app.get('/users/:userId/login-attempts', async(req,res,next)=>{
    const loginAttempt = await User.aggregate([
        { $match: { _id: mongoose.Types.ObjectId(req.params.userId) } },
        { $unwind: '$loginAttempts' },
        { $replaceRoot: { newRoot: '$loginAttempts' } },
    ])
})

I want when the operator $match does not find a matching document to throw an error to report something at least, to show the client a real message that the req.params.id he's looking for does not exist in the first stage.

But how?

about 4 years ago · Santiago Trujillo
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