Lesson 20. Updating and Deleting your Models

 

In lesson 19, you built create and read functionality for your models. Now it’s time to complete the CRUD methods. In this lesson, you add the routes, actions, and views for the update and delete functions. First, you create a form to edit the attributes of existing users. Then you manage the modified data in an update action. At the end of the lesson, you implement a quick way to delete users from your users index page. To start, make sure that your MongoDB server is running by entering mongod in a terminal window.

This lesson covers

  • Constructing a model edit form
  • Updating user records in your database
  • Deleting user records
Consider this

Your recipe application is ready to accept new users, but you’re getting complaints that multiple unnecessary accounts were made and that some users accidentally typed the wrong email address. With the update and delete CRUD functions, you’ll be able to clear unwanted records and modify existing ones to persist in your application.

20.1. Building the edit user form

To update a user’s information, you use some Mongoose methods in a specific update action. First, though, you create a form to edit user information. The form looks like the one in create.js, but the form’s action points to users/:id/update instead of users/create because you want your route to indicate that the form’s contents are updating an existing user, not creating a new one.

20.2. Updating users from a view

20.3. Deleting users with the delete action

Summary