aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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...');