I'm trying to find elements that contain specific values and push a certain value of those elements into a different array using a forEach loop.
app.get('/', async (req, res) => {
const descriptions = await Description.find({});
let tempDescriptions = [];
let categoryClasses = [];
descriptions.forEach(element => tempDescriptions.push(...element.category));
categories = [...new Set(tempDescriptions)]
categories.forEach async (function(element) {
const description = await Description.findOne({category : element});
categoryClasses.push(description.category[0]);
});
console.log(categoryClasses);
res.render('home', { categories, categoryClasses })
});
But after I run this code, categoryClasses is still an empty array. I kind of get why this is happening, but I have no clue how to fix this. Any help would be greatly appreciated!