When I search in MongoDB, I encounter validation issues. If it finds the information I am looking for, it returns the result. However, if it doesn't find anything, it returns an empty document. Here is my code and query in Postman: image
exports.search = (req, res) => {
// Extract the search string
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: 'success',
document
});
});
}