From d1eae33cfb68dd8b14d8e10e2aef6d76c1e3d337 Mon Sep 17 00:00:00 2001 From: JayWll Date: Thu, 16 Jul 2020 12:02:36 -0600 Subject: [PATCH] Video 119: Filtering Data --- task-manager/src/routers/task.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/task-manager/src/routers/task.js b/task-manager/src/routers/task.js index dac94a3..5ae68a0 100644 --- a/task-manager/src/routers/task.js +++ b/task-manager/src/routers/task.js @@ -17,9 +17,19 @@ router.post('/tasks', auth, async (req, res) => { } }) +// GET /tasks?completed=true router.get('/tasks', auth, async (req, res) => { + const match = {} + + if (req.query.completed) { + match.completed = req.query.completed === 'true' + } + try { - await req.user.populate('tasks').execPopulate() + await req.user.populate({ + path: 'tasks', + match + }).execPopulate() res.send(req.user.tasks) } catch (e) { res.status(500).send()