aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-05-08 20:30:45 +0200
committerMateja <mail@matejamaric.com>2021-05-08 20:30:45 +0200
commit9a78a9e480631106fa437d7a53b854c83cc5db02 (patch)
treee48408948e7fdd92d9ca65634123b49b7b98a569
parent7d433d186eda4ec5cbd5cc851491a07691551839 (diff)
downloadnode-playground-9a78a9e480631106fa437d7a53b854c83cc5db02.tar.gz
node-playground-9a78a9e480631106fa437d7a53b854c83cc5db02.zip
Error handling.
-rw-r--r--controllers/post.js13
-rw-r--r--controllers/user.js12
2 files changed, 20 insertions, 5 deletions
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...');