Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

80
Views
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 answers
Answer question

0

You have a typo, lenght instead of length:

if (!document || document.lenght <= 0) {
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!