Created
February 14, 2023 02:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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