WEBVTT 0 00:00.170 --> 00:00.450 All right. 1 00:00.510 --> 00:07.740 Now it's time to move on to handling PUT requests on a specific resource. 2 00:07.740 --> 00:15.870 Say for example if the client wanted to submit an entire new version of the article on Jack Bauer then 3 00:15.900 --> 00:21.930 they would do that via making a PUT request to this particular route. 4 00:22.290 --> 00:29.460 So if we did that in a standalone way then we would write app.put and then we would specify the route 5 00:29.550 --> 00:33.480 and also the callback function to handle that PUT request. 6 00:33.480 --> 00:39.210 But in our case we're simply going to chain this put method onto our app.route. 7 00:39.420 --> 00:46.350 So over here just below the .get we're going to remove that semicolon to say that, "Hey, wait a minute. 8 00:46.350 --> 00:48.520 We're not done changing methods yet. 9 00:48.660 --> 00:52.860 We're going to chain another one." And this is of course the put method. 10 00:52.860 --> 00:54.470 So it's going to the same route so 11 00:54.480 --> 00:57.150 we don't have to specify that all over again. 12 00:57.360 --> 01:04.770 And in this case we're going to add in our callback method, our req and res and then inside here, we're 13 01:04.770 --> 01:12.360 going to work with our MongoDB to replace a particular document inside our articles collection with 14 01:12.360 --> 01:14.700 whatever is sent over by the client. 15 01:14.700 --> 01:18.570 So in order to do this, we're essentially doing update from CRUD. 16 01:18.780 --> 01:23.910 And we're going to use the update method that's available to us through Mongoose. 17 01:23.910 --> 01:30.060 Now the model name in this case will be article and then we're going to call the Update method, specify 18 01:30.090 --> 01:31.020 the condition 19 01:31.020 --> 01:33.860 so which article do we want to update, 20 01:33.990 --> 01:37.230 and then the actual updates that need to be made 21 01:37.230 --> 01:42.190 and finally we're going to set a property called overwrite to be true. 22 01:42.300 --> 01:51.150 And this tells MongoDB that we want to overwrite the entire document with what is specified in the 23 01:51.210 --> 01:52.180 update. 24 01:52.200 --> 01:58.360 Now if we leave this part out or leave it as false then it will only update a particular field. 25 01:58.500 --> 02:00.390 And we're going to see this very very shortly. 26 02:00.420 --> 02:03.210 So let's put this into action and show you what I mean. 27 02:03.210 --> 02:12.660 So inside our put method we're going to tap in to the article model and we're going to call update. Now 28 02:12.690 --> 02:13.590 inside update 29 02:13.590 --> 02:16.140 there's three things that we have to specify. 30 02:16.170 --> 02:22.170 The first being the conditions upon which we want to perform this update method, 31 02:22.170 --> 02:26.850 so which articles specifically do we actually want to update. 32 02:26.850 --> 02:30.770 Well in this case it's pretty much the same as what we have over here. 33 02:30.810 --> 02:37.320 We're going to search through our articles collection for one that has a title that matches the one 34 02:37.320 --> 02:42.030 that is requested by the client inside the URL parameter. 35 02:42.030 --> 02:45.660 So we're pretty much going to write the same here for our condition. 36 02:45.660 --> 02:53.490 So title matches the request.params.articleTitle parameter. 37 02:53.500 --> 02:56.730 Now that's the first one out of the way and that's the condition. 38 02:56.730 --> 03:02.730 The next one that we have to put into this update method is the actual update that we want to make. 39 03:02.730 --> 03:08.850 So in this sense it's kind of like that POST request that we made previously where the client will submit 40 03:08.940 --> 03:16.500 some new content through the body of the request and that can contain the title and the content and 41 03:16.500 --> 03:25.290 we'll use this information inside the request to update our article that we found through this search. 42 03:25.320 --> 03:32.610 So over here I'm going to open up another set of curly braces and we're going to add in the title as 43 03:32.610 --> 03:35.490 the req.body 44 03:35.490 --> 03:42.000 so this is body-parser passing our request and looking for that thing that's sent over through an 45 03:42.010 --> 03:45.410 HTML form for example. And we'll give it some names 46 03:45.420 --> 03:51.940 so it's going to be req.body.title and also we're going to update the content using req 47 03:51.940 --> 03:54.630 .body.content. 48 03:54.780 --> 04:02.490 So now that means when we make our request through Postman not only do we have to change this to PUT, 49 04:02.700 --> 04:07.530 we also have to change what's inside the body here. 50 04:08.100 --> 04:15.720 So we're specifying to keys: the title and the content, and both of those have to match exactly what we 51 04:15.720 --> 04:16.520 wrote here, 52 04:16.530 --> 04:22.160 title and content because those are the variables that will be passed out of the request. 53 04:22.170 --> 04:25.030 Now we're going to give it some other titles to it. 54 04:25.050 --> 04:33.270 Let's say that we decide to change the Jack Bauer article to Chuck Norris and there's actually also 55 04:33.390 --> 04:35.400 a Chuck Norris facts 56 04:35.460 --> 04:36.720 top hundred. 57 04:36.840 --> 04:39.180 I don't know who makes all these websites. 58 04:39.420 --> 04:40.800 So I'm just gonna copy this one, 59 04:40.800 --> 04:46.440 "The first rule of Chuck Norris is you do not talk about Chuck Norris" has a bit of reference to fight 60 04:46.440 --> 04:47.180 Club in there. 61 04:47.550 --> 04:55.620 So let's say that I have decided to change my article for Jack Bauer to have a different title and a 62 04:55.620 --> 04:57.060 different content. 63 04:57.120 --> 05:01.110 So I make my PUT request to the route that his articles 64 05:01.130 --> 05:03.070 /Jack%20Bauer 65 05:03.200 --> 05:10.730 and when I hit send my request will be handled by this part of the code in my server and it will look 66 05:10.820 --> 05:14.550 to see what is that parameter that is over here. 67 05:14.840 --> 05:21.710 So in my case it's Jack Bauer and then it's going to search all of my articles for something that has 68 05:21.710 --> 05:27.920 a title of Jack Bauer and then it's going to replace the title with the new title and the content with 69 05:27.920 --> 05:33.180 the new content that we've specified in the body of the request. 70 05:33.200 --> 05:40.550 Finally all that we have to do is add the finishing touch to this update method which has to include 71 05:40.550 --> 05:43.510 that "overwrite is true" argument. 72 05:43.700 --> 05:50.570 Now the reason why we need to add this is because by default if we were to update our document using 73 05:50.600 --> 05:56.720 MongoDB, then this is already enabled. It will overwrite things 74 05:56.720 --> 06:04.520 if you don't include all of the fields in your update. But by using Mongoose it deems it necessary to 75 06:04.520 --> 06:06.590 prevent this overwriting. 76 06:06.680 --> 06:12.670 So by default Mongoose will prevent properties being overwritten and deleted. 77 06:12.830 --> 06:18.620 So let's go into here and add that "overwrite: true". 78 06:19.190 --> 06:27.890 So now we're finally ready to add in our callback and check to see if there were any errors. And if there 79 06:27.890 --> 06:39.560 were no errors then it will simply res.send "Successfully updated article." and we can close off our 80 06:39.560 --> 06:46.760 method and as well as cap off our chained method, the route .get and .put. 81 06:46.760 --> 06:54.140 So now if we save our app.js and go in to our Robo 3T you can see that at the moment my last 82 06:54.290 --> 07:02.030 document in my articles collection has a title of Jack Bauer and a content of "Jack Bauer once stepped 83 07:02.030 --> 07:05.660 into quicksand and the quicksand could not escape." 84 07:06.080 --> 07:14.420 So now if I go ahead and submit my PUT request to articles/Jack%20Bauer, it should search 85 07:14.510 --> 07:22.070 through my articles for a document that has a title of Jack Bauer and it will place the title and the 86 07:22.070 --> 07:24.350 content of that document. 87 07:24.410 --> 07:30.640 So let's go ahead and hit send and we get "Successfully updated article." 88 07:30.950 --> 07:38.390 And now if we go into Robo 3T and we view our documents you can see that our last document no longer 89 07:38.390 --> 07:42.560 has a title of Jack Bauer and the content has also been changed. 90 07:43.130 --> 07:47.480 Now here comes the interesting part of the PUT request. 91 07:47.690 --> 07:50.380 If we only changed one thing, 92 07:50.570 --> 07:58.910 let's say I delete this title, so I'd no longer send a new title along with my PUT request and instead 93 07:58.910 --> 08:02.080 I only sent a new value for the content property. 94 08:02.090 --> 08:08.970 Let's go and restore this so that our server can actually find a document with the title of Jack Bauer. 95 08:09.050 --> 08:14.900 So I'm gonna go ahead and remove all documents and if we now view documents, there's nothing left in 96 08:14.910 --> 08:22.790 here but if I go back into my GitHub project and repopulate the database, then I now should have restored 97 08:23.180 --> 08:25.520 all of my changes that I made before. 98 08:25.520 --> 08:29.980 So now we should be able to find a document as a title of Jack Bauer. 99 08:30.590 --> 08:34.160 And in this case I'm only updating the content, 100 08:34.160 --> 08:37.490 I have no value for the title in here. 101 08:37.490 --> 08:40.310 So what do you think will happen when I hit send? 102 08:43.150 --> 08:48.650 So I get a "Successfully updated article." message being sent back. 103 08:48.850 --> 08:57.100 And if we check Robo 3T, if we refresh our articles, you can see that I now no longer even have a title 104 08:57.100 --> 08:57.940 property. 105 08:58.030 --> 09:07.240 It's just been wiped clean because as we saw before, a PUT request is meant to replace the entire 106 09:07.240 --> 09:08.020 resource. 107 09:08.050 --> 09:12.730 So in this case the broken bicycle is replaced by a brand new bicycle. 108 09:13.000 --> 09:21.160 But in my case here my document on Jack Bauer has been entirely replaced by the values that I have supplied 109 09:21.430 --> 09:23.230 in my PUT request. 110 09:23.380 --> 09:26.200 So where does this overwrite argument come from? 111 09:26.200 --> 09:32.980 Well there's a really good post on Stack Overflow that actually addresses this in a lot of detail and 112 09:33.010 --> 09:37.360 I'll link to it in the course resources for this lesson. 113 09:37.360 --> 09:43.510 But the important thing is to realize that when you're using Mongoose it's actually doing some stuff 114 09:43.540 --> 09:46.990 behind the scenes, things that you might want. 115 09:47.080 --> 09:55.420 So normally when we run the method update which is a Mongoose method it will override the default Mongo 116 09:55.420 --> 10:01.000 DB behavior which is to overwrite everything with the values that the client provides. 117 10:01.000 --> 10:06.860 So namely if there wasn't a title then the title property would be completely wiped out. 118 10:07.090 --> 10:13.080 But in order to suppress this behavior from Mongoose that's why we're adding this overwrite property. 119 10:13.180 --> 10:14.250 So have a read of this 120 10:14.290 --> 10:17.260 if you want a more in-depth explanation of this. 121 10:17.350 --> 10:24.610 So what if we don't want to replace our entire document with only the values that were submitted in 122 10:24.610 --> 10:25.720 the PUT request? 123 10:25.720 --> 10:28.760 Well that is where the PATCH request comes in. 124 10:28.780 --> 10:31.860 So for all of that and more, I'll see you on the next lesson.