Hey there! I have this GET function that uses async to find a specific "Category" mongoose Schema that the user just clicked on, along with another "Tool" mongoose Schema which fetches all tools from my database and sends them both to a rendered page.
I'm wondering if there’s a way to add filtering to my Tool.find so it only retrieves tools with the same category property (Tool.category) as the Category (Category.name) the user clicked on?
Here's the GET function:
router.get("/catalog/:id", function (req, res, next) {
let output = {
category: [],
tools: []
};
async.parallel([
function (cb) {
Category.findById(req.params.id).exec(function (err, foundCategory) {
if (err || !foundCategory) {
req.flash("error", "No category found.");
return res.redirect("back");
} else {
output.category = foundCategory;
cb(null, foundCategory);
}
});
},
function (cb) {
Tool.find({}, function (err, foundTools) {
if (err || !foundTools) {
req.flash("error", "No tools were found.");
return res.redirect("back");
} else {
output.tools = foundTools;
cb(null, foundTools);
}
});
}
], function done(err, results) {
if (err) {
res.json(err.message);
} else {
res.render("tools/catalog-items", {
category: output.category,
tools: output.tools
});
}
});
});