06 - Update an Item
Many great apps and sites do not have the ability to update things that have been posted, such as Twitter and Snapchat. These are known as CRD apps. In our case, we want a full CRUD app, so let's get to work. This is the last major addition we need for our server to be complete.
The Code
Add the following at the bottom of authtestcontroller.js
, right above the export statement.
Analysis
PUT
is one of the HTTP verbs that has to be weird by not telling you what it does.PUT
replaces whatever is already there with what we give it. In other words,PUT
means update.To make it easier on the user, we use
update
in our route. We also allow a variable (id
) to be passed through the URL again.The parameter taken from the URL.
Our data we want to put into the database, replacing what already exists.
update
is a Sequelize method which takes two arguments.First argument of
update
. Contains an object holding the new value we want to edit into the database.Second argument of
update
. Tells Sequelize where to place the new data if a match is found.Callback function. Runs if update is successful, and returns the data entered.
Callback function. Runs if update is not successful, and returns the error message.
And that's it! Our server is done! Let's do some final testing with Postman, and then move on to setting up our client to navigate these authenticated routes.
Last updated