Home > Enterprise >  Problems with if in search mongoose
Problems with if in search mongoose

Time:01-07

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
        });
    });
}

CodePudding user response:

You have a typo, lenght instead of length:

if (!document || document.lenght <= 0) {
  •  Tags:  
  • Related