Video Course

Create a CRUD App with Node and MongoDB: Lesson 16 of 17

Editing Events

Up Next

Deleting Events

Autoplaying in 7 seconds!

Cancel

Editing events is going to use two of the mongoose methods that we used earlier. We'll use Event.findOne() and the .save() method.

Here's an example:

// finding a current event
Event.findOne({ slug: req.params.slug }, (err, event) => {
  // updating that event
  event.name        = req.body.name;
  event.description = req.body.description;

  event.save((err) => {
    if (err)
      throw err;

    // success flash message
    // redirect back to the /events
    req.flash('success', 'Successfully updated event.');
    res.redirect('/events');
  });
});

Chris Sevilleja

157 posts

Co-founder of Scotch.io. Slapping the keyboard until something good happens.