I have this
router.get('/documents', function(req, res, next) {
Document.find(function(err, doc){
res.render('documents', { doc: doc });
});
});
router.post('/documents', function(req, res, next) {
// Create function
req.body.newdocs.forEach(function(newdoc) {
Document.create({title : newdoc.title, desc: newdoc.desc}, function (err, ndoc) {
if (err) {
res.flash('error', "Error. Try again.");
res.redirect('/documents');
}
});
});
// Update function
req.body.docs.forEach(function(doc) {
var d = {
title: doc.title,
desc: doc.desc
}
Document.update({'_id': doc.id}, d, {overwrite: true}, function(err, raw) {
if (err) return handleError(err);
});
});
res.redirect('/documents');
});
When I create some documents, post them (documents are created in database) and the redirection works. So I get the page, but I only have the documents before the post. And when I refresh the page (GET the page one more time), I have well the new documents.
Do you have some ideas of explanation, to fix this ?