I have a web page view that requires two sets of mongoose documents, one for "complete tasks" and one for "incomplete tasks."
Here is my current code:
router.get('/', (req, res) => {
Task.find({'completed': false}) // Finding all incomplete tasks
.exec()
.then((incompleteTasks) => {
Task.find({'completed': true}) // Finding all complete tasks
.exec()
.then((completeTasks) => {
res.render('tasks/tasks', {
title: 'Tasks',
incompleteTasks: incompleteTasks,
completeTasks: completeTasks,
});
});
});
});
Is there a way to make this more elegant? Right now, it isn't too bad, but if I had to split up my tasks into more than sets, the code would get pretty messy.
What I thought of was to make the code have the following structure:
let incompleteTasks = /* Get INCOMPLETE tasks */
let completeTasks = /* Get COMPLETE tasks */
res.render('tasks/tasks', {
title: 'Tasks',
incompleteTasks: incompleteTasks,
completeTasks: completeTasks,
});
However, because of the asynchronous nature of mongoose queries, I'm not sure how to do this.
Any suggestions?