3 works

Express Post Request to Edit Post


dashboard+ Project

Posted by @mishka #nodejs

Code snippet:

router.post('/:id/edit', auth.requireLogin, (req, res, next) => {
  Post.findByIdAndUpdate(req.params.id, req.body, function(err, post) {
    if(err) { console.error(err) };

     res.redirect(`/`+req.params.id);
  });
});
content_copyCopy to Clipboard

More details:

The url in the first line must be the same as the action in the edit form. Be aware that the findByIdAnd Update method erases previous data before replacing it.


>> Browse more code snippets

more_vert