From 9a78a9e480631106fa437d7a53b854c83cc5db02 Mon Sep 17 00:00:00 2001 From: Mateja Date: Sat, 8 May 2021 20:30:45 +0200 Subject: Error handling. --- controllers/post.js | 13 +++++++++++-- controllers/user.js | 12 +++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) (limited to 'controllers') diff --git a/controllers/post.js b/controllers/post.js index 09bbdec..45da525 100644 --- a/controllers/post.js +++ b/controllers/post.js @@ -4,6 +4,7 @@ module.exports = { index(req, res) { Post.find().lean().exec((err, posts) => { + if (err) console.log(err); res.render('home', { title: 'Home Page', auth: req.isAuthenticated(), @@ -26,11 +27,19 @@ module.exports = { 'title': req.body.title, 'text': req.body.text }); - newPost.save().then(() => res.redirect('/')); + newPost.save() + .then(() => res.redirect('/')) + .catch(err => { + console.log(err); + res.redirect('/new-post'); + }); }, destroy(req, res) { - Post.findByIdAndRemove(req.params.id).then(() => res.redirect('/')); + Post.findByIdAndRemove(req.params.id, (err, post) => { + if (err) console.log(err); + res.redirect('/'); + }); } }; diff --git a/controllers/user.js b/controllers/user.js index c64bfa0..21ab829 100644 --- a/controllers/user.js +++ b/controllers/user.js @@ -11,9 +11,15 @@ module.exports = { email: req.body.email, password: bcrypt.hashSync(req.body.password) }); - newUser.save().then(() => { - req.login(newUser, () => res.redirect('/new-post')); - }); + newUser.save() + .then(() => { + req.login(newUser, () => res.redirect('/new-post')); + }) + .catch(err => { + console.log(err); + req.flash('msg', 'Validation errors...'); + req.redirect('/register'); + }); } else { req.flash('msg', 'Validation errors...'); -- cgit v1.2.3