Skip to content

Instantly share code, notes, and snippets.

@epjuan21
Created February 14, 2023 02:38
Show Gist options
  • Save epjuan21/6bce05db74d48b8ab933c7fa62b304a1 to your computer and use it in GitHub Desktop.
Save epjuan21/6bce05db74d48b8ab933c7fa62b304a1 to your computer and use it in GitHub Desktop.
Obtener servicios según termino de búsqueda en Nombre y Descripción. En caso de no enviar parámetro se consultan todos los datos
exports = async function(payload, response){
const { q } = payload.query;
let searchTerm = "";
let query = {};
let findResult;
if (typeof q !== 'undefined' && q !== "") {
searchTerm = q;
}
if (searchTerm !== "") {
query = {
$or: [
{ Nombre: {$regex: `${searchTerm}`, $options: 'i'} },
{ Descripcion: {$regex: `${searchTerm}`, $options: 'i'} }
]
};
}
const mongodb = context.services.get("mongodb-atlas");
const serviciosCollection = mongodb.db("referencias").collection("Servicios");
try {
findResult = await serviciosCollection.find(query)
} catch(err) {
console.log("Error occurred while executing findOne:", err.message);
return { error: err.message };
}
return { result: findResult };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment