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

83
Vistas
Problems with if in search mongoose

I have a problem with the validation after performing the search in mongodb, if it makes the search for the information that I consult, but when not, it returns an empty document, this is my code and my query in postman > image

exports.search = (req, res) => {
    //sacar el string a buscar
    var searchString = req.params.search;
    //find or
    Document.find({ "$or": [
            { "num_oficio": { "$regex": searchString, "$options": "i"}},
            { "ins_juridico": { "$regex": searchString, "$options": "i"}},
            { "fecha_recepcion": { "$regex": searchString, "$options": "i"}},
            { "remitido": { "$regex": searchString, "$options": "i"}},
            { "origen": { "$regex": searchString, "$options": "i"}},
            { "direccion": { "$regex": searchString, "$options": "i"}},
            { "director": { "$regex": searchString, "$options": "i"}},
            { "asunto": { "$regex": searchString, "$options": "i"}},
            { "estatus": { "$regex": searchString, "$options": "i"}},
            { "observacion": { "$regex": searchString, "$options": "i"}}
    ]}).sort([['fecha_recepcion', 'descending']]).exec((err, document) => {
        if (err) {
            return res.status(500).send({
                status: 'error',
                message: 'Error en la peticion'
            });
        }
        if (!document || document.lenght <= 0) {
            return res.status(404).send({
                status: 'error',
                message: 'No hay documentos relacionados con tu busqueda!'
            });
        }
        return res.status(200).send({
            status: 'succes',
            document
        });
    });
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have a typo, lenght instead of length:

if (!document || document.lenght <= 0) {
about 4 years ago · Juan Pablo Isaza Denunciar
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