Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
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 Respostas
Responde à pergunta

0

You have a typo, lenght instead of length:

if (!document || document.lenght <= 0) {
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda