From cbada51856288b4cdaed464bc15165f9c1f23883 Mon Sep 17 00:00:00 2001 From: Ellen Lee Date: Fri, 19 Jun 2020 03:10:52 +0800 Subject: [PATCH] add delete todo function --- routes/modules/todos.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/routes/modules/todos.js b/routes/modules/todos.js index 6aabfc5..83aefa5 100644 --- a/routes/modules/todos.js +++ b/routes/modules/todos.js @@ -53,4 +53,14 @@ router.put('/:id', (req, res) => { .catch(error => console.log(error)) }) +router.delete('/:id', (req, res) => { + const UserId = req.user.id + const id = req.params.id + + return Todo.findOne({ where: { id, UserId } }) + .then(todo => todo.destroy()) + .then(() => res.redirect('/')) + .catch(error => console.log(error)) +}) + module.exports = router