I have a working function that can handle multiple collections, but I am trying to save on redundancy and call the find() and then() calls outside the switch case statements since they all use the same thing after the collection has been identified. When I run this tho there is a 500 error. What am I doing wrong with the find(), then() calls??
export function get_epdAccess(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
let collectionName = request.path[0];
let searchItem = request.path[1];
switch (collectionName) {
case ‘patientInfo’:
wixData.query(“Patient_Info”) //call for info inside the Patient_Info Collection
.eq(“cassette_id”, searchItem) //check the first value with items in the Patient_ID column
break ;
case ‘doctorDatabase’:
wixData.query(“Doctor_Database”)
.eq(“npi_id”, searchItem) //check the first value with items in the Patient_ID column
break ;
case ‘latinAbbreviations’:
wixData.query(“Latin_Abbreviations”)
.eq(“title”, searchItem) //check the first value with items in the Patient_ID column
break ;
case ‘locationDatabase’:
wixData.query(“Location_Database”)
.eq(“title”, searchItem) //check the first value with items in the Patient_ID column
break ;
case ‘pillDatabase’:
wixData.query(“Pill_Database”)
.eq(“title”, searchItem) //check the first value with items in the Patient_ID column
break ;
default :
options.body = {
“error”: ${request.path[0]} Collection was not found or url is missing an argument
};
return notFound(options);
}
wixData.find() // <------------------ Issue starts here I think
.then((results) => {
// matching items were found
if (results.items.length > 0) {
options.body = {
“items”: results.items
};
return ok(options);
}
// no matching items found
options.body = {
“error”: ${searchItem} was not found inside the ${collectionName} Collection
};
return notFound(options);
})
// something went wrong
. catch ((error) => {
options.body = {
“error”: error
};
return serverError(options);
});
}