All right.
Now it's time to move on to handling PUT requests on a specific resource.
Say for example if the client wanted to submit an entire new version of the article on Jack Bauer then
they would do that via making a PUT request to this particular route.
So if we did that in a standalone way then we would write app.put and then we would specify the route
and also the callback function to handle that PUT request.
But in our case we're simply going to chain this put method onto our app.route.
So over here just below the .get we're going to remove that semicolon to say that, "Hey, wait a minute.
We're not done changing methods yet.
We're going to chain another one." And this is of course the put method.
So it's going to the same route so
we don't have to specify that all over again.
And in this case we're going to add in our callback method, our req and res and then inside here, we're
going to work with our MongoDB to replace a particular document inside our articles collection with
whatever is sent over by the client.
So in order to do this, we're essentially doing update from CRUD.
And we're going to use the update method that's available to us through Mongoose.
Now the model name in this case will be article and then we're going to call the Update method, specify
the condition
so which article do we want to update,
and then the actual updates that need to be made
and finally we're going to set a property called overwrite to be true.
And this tells MongoDB that we want to overwrite the entire document with what is specified in the
update.
Now if we leave this part out or leave it as false then it will only update a particular field.
And we're going to see this very very shortly.
So let's put this into action and show you what I mean.
So inside our put method we're going to tap in to the article model and we're going to call update. Now
inside update
there's three things that we have to specify.
The first being the conditions upon which we want to perform this update method,
so which articles specifically do we actually want to update.
Well in this case it's pretty much the same as what we have over here.
We're going to search through our articles collection for one that has a title that matches the one
that is requested by the client inside the URL parameter.
So we're pretty much going to write the same here for our condition.
So title matches the request.params.articleTitle parameter.
Now that's the first one out of the way and that's the condition.
The next one that we have to put into this update method is the actual update that we want to make.
So in this sense it's kind of like that POST request that we made previously where the client will submit
some new content through the body of the request and that can contain the title and the content and
we'll use this information inside the request to update our article that we found through this search.
So over here I'm going to open up another set of curly braces and we're going to add in the title as
the req.body
so this is body-parser passing our request and looking for that thing that's sent over through an
HTML form for example. And we'll give it some names
so it's going to be req.body.title and also we're going to update the content using req
.body.content.
So now that means when we make our request through Postman not only do we have to change this to PUT,
we also have to change what's inside the body here.
So we're specifying to keys: the title and the content, and both of those have to match exactly what we
wrote here,
title and content because those are the variables that will be passed out of the request.
Now we're going to give it some other titles to it.
Let's say that we decide to change the Jack Bauer article to Chuck Norris and there's actually also
a Chuck Norris facts
top hundred.
I don't know who makes all these websites.
So I'm just gonna copy this one,
"The first rule of Chuck Norris is you do not talk about Chuck Norris" has a bit of reference to fight
Club in there.
So let's say that I have decided to change my article for Jack Bauer to have a different title and a
different content.
So I make my PUT request to the route that his articles
/Jack%20Bauer
and when I hit send my request will be handled by this part of the code in my server and it will look
to see what is that parameter that is over here.
So in my case it's Jack Bauer and then it's going to search all of my articles for something that has
a title of Jack Bauer and then it's going to replace the title with the new title and the content with
the new content that we've specified in the body of the request.
Finally all that we have to do is add the finishing touch to this update method which has to include
that "overwrite is true" argument.
Now the reason why we need to add this is because by default if we were to update our document using
MongoDB, then this is already enabled. It will overwrite things
if you don't include all of the fields in your update. But by using Mongoose it deems it necessary to
prevent this overwriting.
So by default Mongoose will prevent properties being overwritten and deleted.
So let's go into here and add that "overwrite: true".
So now we're finally ready to add in our callback and check to see if there were any errors. And if there
were no errors then it will simply res.send "Successfully updated article." and we can close off our
method and as well as cap off our chained method, the route .get and .put.
So now if we save our app.js and go in to our Robo 3T you can see that at the moment my last
document in my articles collection has a title of Jack Bauer and a content of "Jack Bauer once stepped
into quicksand and the quicksand could not escape."
So now if I go ahead and submit my PUT request to articles/Jack%20Bauer, it should search
through my articles for a document that has a title of Jack Bauer and it will place the title and the
content of that document.
So let's go ahead and hit send and we get "Successfully updated article."
And now if we go into Robo 3T and we view our documents you can see that our last document no longer
has a title of Jack Bauer and the content has also been changed.
Now here comes the interesting part of the PUT request.
If we only changed one thing,
let's say I delete this title, so I'd no longer send a new title along with my PUT request and instead
I only sent a new value for the content property.
Let's go and restore this so that our server can actually find a document with the title of Jack Bauer.
So I'm gonna go ahead and remove all documents and if we now view documents, there's nothing left in
here but if I go back into my GitHub project and repopulate the database, then I now should have restored
all of my changes that I made before.
So now we should be able to find a document as a title of Jack Bauer.
And in this case I'm only updating the content,
I have no value for the title in here.
So what do you think will happen when I hit send?
So I get a "Successfully updated article." message being sent back.
And if we check Robo 3T, if we refresh our articles, you can see that I now no longer even have a title
property.
It's just been wiped clean because as we saw before, a PUT request is meant to replace the entire
resource.
So in this case the broken bicycle is replaced by a brand new bicycle.
But in my case here my document on Jack Bauer has been entirely replaced by the values that I have supplied
in my PUT request.
So where does this overwrite argument come from?
Well there's a really good post on Stack Overflow that actually addresses this in a lot of detail and
I'll link to it in the course resources for this lesson.
But the important thing is to realize that when you're using Mongoose it's actually doing some stuff
behind the scenes, things that you might want.
So normally when we run the method update which is a Mongoose method it will override the default Mongo
DB behavior which is to overwrite everything with the values that the client provides.
So namely if there wasn't a title then the title property would be completely wiped out.
But in order to suppress this behavior from Mongoose that's why we're adding this overwrite property.
So have a read of this
if you want a more in-depth explanation of this.
So what if we don't want to replace our entire document with only the values that were submitted in
the PUT request?
Well that is where the PATCH request comes in.
So for all of that and more, I'll see you on the next lesson.