- [Instructor] Now when we need to update our data,
we need to create a PUT endpoint.
And this is what we'll focus on in this video.
So you want to get back to the CRM controller
in the Controllers folder.
And we're going to, again, Copy and Paste line 26 through 33,
and Paste it below,
and we're going to call this new function updateContact.
Now, the arguments stay the same,
and here we are also using Contact, or the connection,
the Mongoose connection, to our Mongo database,
and now the function that we're going to use
is called findOne, and update.
And the parameters, or the arguments,
of this specific one, are going to change.
So let's remove this first area here,
and what we're going to do is first insert the curly braces,
and what we need to do is pass the Contact idea
from the params in the request
to the idea that we want to update.
But just to make sure that we're using the right stuff,
let's go back to Postman,
and take a look at how the ID is represented here.
So, it's an underscore ID,
and then we're going to pass this ID
inside of the params of the request.
So we want to make sure we're using the same key
when we're creating the code here.
So, what we're going to do is pass underscore ID
as the key that we want to use,
and we're passing the params like we've done in a pass.
So, the same thing as what we have here.
So, again, you want to make sure you're using the same
Contact ID that you have in your routes,
pretty much like what we have here.
So, capital I and D, and the same thing here.
So we're good with that.
Now we've passed the right data to the right key.
Now we also need to pass the body of the request,
so we have the data that we want to update,
and then we need to pass a few options.
So, what we need to pass for the options,
our two options that are very important for this request.
The first one is called new, and we want it at true.
So this option will basically tell the Mongo database
to return the new object, the updated contact,
as opposed to the old one.
If you put this as false,
it's going to return the old object,
not the updated one.
So, in a sense,
you're not going to have any idea if your object has been,
or your contact has been updated.
So you want to make sure you turn this to true.
The next one is basically an option that we need to put,
in order to use most recent functions,
as opposed to deprecated ones,
so it's called USE find,
and modify,
and we want to turn it to false.
With that you're not going to get any errors in your request
with a deprecated message.
Everything else is the same,
so we're still passing error contact,
and we want to make sure that we have a comma here in-between.
And, if error, send error,
when you got an object, send it back,
and obviously you want to send the new object.
All right, so, this is all good on this end,
let's pass this to the route.
So this is going to go in the PUT route,
so let's import it first.
So we're going to go and import it, delete function first,
so it's called update Contact,
and then we need to pass this to the PUT request.
So, again, I'm going to highlight the second parentheses,
to the one before last, like so, and remove them.
And then pass update Contact.
Okay, so now everything is working properly,
let's go to Postman and test this.
So, let's update this Jasmine contact.
So let's Copy the ID,
and let's do forward slash and pass the ID,
and we want to use the PUT method,
and then let's change something in here.
So we're going to change the phone number.
So instead of five, five, five,
we'll do six, six, six, six, six, six, six,
six, six.
I think there's seven numbers in there.
One is missing, there you go.
All right, so now we're going to pass this brand new data,
and you don't have to send everything else in the body,
so you can omit to send all the other stuff,
and just send what you want to change,
and this is a PUT request,
so it's going to send you the data
with the change once it's done.
All right, so let's send this.
And now we've got the same object with all the same data,
except the phone number,
which has been changed with our PUT command.
And then if we go back to our server,
everything is running properly.
And by the way,
if you didn't have this thermodiffusion equals to false,
you would have a deprecated information or error here,
so you want to make sure to include that.
One more thing, over time, libraries change.
When you see deprecation messages, more often than not,
you're going to have some alert or messages
to tell you what to do or what to use
to actually avoid to have this deprecation message.
You want to pay attention to these messages
and change whatever needs to be changed in your code.
So now we can update our data with a PUT request,
so let's move on to the next step.