//========= POST ========//
// Process Form
app.post('/ideas', (req, res) => {
let errors = [];
// Add Server-Side Validation
if (!req.body.title) {
errors.push({
text: 'Please add a title'
});
}
if (!req.body.details) {
errors.push({
text: 'Please add some details'
});
}
// Check if there are errors
if (errors.length > 0) {
// Render back html page to send error messages
res.render('ideas/add', {
errors,
title: req.body.title,
details: req.body.details
})
} else {
// Shit happens here
const newUser = {
title: req.body.title,
details: req.body.details
}
// Don't just fucking insert the request body in here.
// It's not a good idea
new Idea(newUser)
.save()
.then(idea => {
res.redirect('/ideas');
})
}
});
//===== END POST ====//