This code works but I don't know how to dismantle this "sausage" code.
async function showAllFilters(qry, arrayParams) {
// params is []
let result;
try {
result = await db.query(qry, arrayParams);
} catch (error) {
console.log("showAllFilters error: ");
console.error(error);
}
return result;
}
router.get('/', function (req, res) { // localhost:5000/api/filter
let qry = "SELECT DISTINCT c.subCatName, (select COUNT(*) from tblPostCatMap as pp where pp.idSubCat = c.idSubCat) as subcatSumme from tblsubcat as c order by c.subCatName asc";
showAllFilters(qry,[])
.then(r => { // first, you get the count of records of all posts and store into count var
var filterJson = {
"categories": {},
"subcategories": {}
};
filterJson["subcategories"] = r;
qry = "SELECT DISTINCT c.catName, (select COUNT(*) from tblPostCatMap as pp where pp.idCat = c.idCategorie) as catSumme from tblcategories as c order by c.catName asc";
showAllFilters(qry, [])
.then(r2 => {
// how to dismantle this?
filterJson["categories"] = r2;
// ...
})
.catch(err => {
console.log("Err in showing all posts with pagination: " + err);
});
})
.catch(err => {
console.log("Err in getting the count of all posts: " + err);
});
});