(solved)http-function multiple collections call

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);
});

}

Hello lgrivera,

Yes, the whole problems starts in this line :
wixData.find() // <-------- Issue starts here I think // THIS LINE

seems like you missed the Query before .Find( ),

wixData.query("DS")
.find()
.then( (results) => { . . . 
});

Take a look at this article for more details : https://www.wix.com/code/reference/wix-data.html#query

hope this helps you.
Mustafa

Thanks! I actually just kept the phrase together in each switch statement case so it could tell which data query it was suppose to do :), example below:


case ‘doctorDatabase’:
return wixData.query(“Doctor_Database”)
.eq(“npi_id”, doctorID) //check the first value with items in the Patient_ID column
.find()
.then((results1) => {
// matching items were found
if (results1.items.length > 0) {
options.body = {
“success”: ‘true’,
“items”: results1.items
};
return ok(options);
}

                                 // no matching items found 
                                    options.body = { 
                                           "success": 'false', 
                                           "error": `${doctorID} was not found inside the ${collectionName} Collection` 
                                    }; 
                                     **return**  notFound(options); 
                                });