WEBVTT 0 00:00.350 --> 00:00.660 All right. 1 00:00.690 --> 00:08.790 So in the last lesson we tackled the PUT request to a specific resource. In this lesson we're going to 2 00:08.790 --> 00:13.400 look at its close cousin or maybe non identical twin brother. 3 00:15.510 --> 00:23.210 And we're going to update a particular article but only the fields that we actually provided data for. 4 00:23.250 --> 00:29.730 So as we saw in the last lesson, if we make a PUT request to a specific resource and we only provide 5 00:29.730 --> 00:37.080 a value for one field then the other field that is neglected or we didn't provide a value for the PUT 6 00:37.080 --> 00:44.130 request will quite literally interpret that as you wanting to make it null or nil or empty or doesn't 7 00:44.130 --> 00:44.550 exist. 8 00:44.610 --> 00:52.530 So in the cases where you only want to update a specific field in a specific document then the HTTP method 9 00:52.530 --> 00:55.380 that you should be using is patch. 10 00:55.380 --> 00:58.480 So let's go and add a PATCH request. 11 00:58.530 --> 01:03.630 So we're going to delete that semicolon from the previous put and this one is going to be the patch 12 01:03.630 --> 01:04.530 method. 13 01:04.710 --> 01:11.130 Now in here we're going to have a function that has a req and res and we're going to open up our 14 01:11.130 --> 01:12.730 curly braces. 15 01:12.780 --> 01:22.260 Now inside this patch function we want to update a particular document in our article collection but 16 01:22.260 --> 01:26.120 we only want to update the field that we provide. 17 01:26.130 --> 01:32.460 So in this case the CRUD operation is still update and we're still going to be using the same update 18 01:32.460 --> 01:37.230 method that we used in the last lesson when we wrote the code to handle PUT request. 19 01:37.470 --> 01:43.740 But in this case when we call the Update method we no longer have that overwrite property set to true 20 01:43.980 --> 01:51.600 and the other addition is that we now have this set flag which tells MongoDB to only update the fields 21 01:51.600 --> 01:53.920 that we have provided updates for. 22 01:53.940 --> 01:56.320 So let's see this in practice. 23 01:56.370 --> 02:04.140 So inside our code we'll target the article model and we'll say update and then the first parameter will 24 02:04.140 --> 02:13.620 be the condition which is going to be the same req.params.articleTitle and that if you 25 02:13.620 --> 02:16.590 remember of course comes from over here. 26 02:16.590 --> 02:19.620 So the route that the user used or the URL. 27 02:19.770 --> 02:26.430 Now the next thing that we have to add are the updates and we're going to use this flag called set. 28 02:26.600 --> 02:33.270 Now the way that the set operator works is that you put in the flag by typing a dollar sign, a set and 29 02:33.270 --> 02:38.490 then a colon. And then what follows are the values for the fields that you want to update and you can 30 02:38.490 --> 02:40.530 add in as many as you want. 31 02:40.530 --> 02:42.550 So this is the way that you would structure it 32 02:42.570 --> 02:45.560 and there's also some really good examples of how you would use it. 33 02:45.570 --> 02:50.730 So I recommend having a read through this reference and I'll link to it in the course resources list 34 02:50.790 --> 02:51.500 as well. 35 02:51.510 --> 02:58.380 So essentially when we come back to our code what we want our code to look like is that if the client 36 02:58.530 --> 03:05.150 only wanted to change the title then this part should read title: something else 37 03:05.160 --> 03:11.830 let's say "Chuck Norris". And if we only wanted to update the content then it would be something like this. 38 03:11.910 --> 03:17.520 And if we wanted to update the content and the title then it would look something like this. 39 03:17.550 --> 03:22.860 Now how can we make that dynamic so that we let the client choose what it is that they actually want 40 03:22.860 --> 03:23.460 to update? 41 03:23.880 --> 03:32.130 Well, we know that when we send a request over, body-parser will pass the request and turn it into an 42 03:32.130 --> 03:32.940 object. 43 03:32.970 --> 03:41.100 So that means that if I added a value for a title and a value for content let's just call that TEST 44 03:41.220 --> 03:49.480 and TEST, then in this case when that gets sent over to my server what I will see when I tap into or 45 03:49.500 --> 03:58.590 log my req.body is it will basically be equal to a Javascript object that has two properties: 46 03:58.680 --> 04:06.090 a title called TEST and another property called content which is also set to TEST and you can verify this 47 04:06.120 --> 04:08.440 if you want to just by console logging. 48 04:08.520 --> 04:10.770 But essentially this is what it would look like. 49 04:10.820 --> 04:19.050 Now if I only had a title that I sent over in my request then it would simply look like this. 50 04:19.530 --> 04:26.040 So as you can see the req.body object is pretty much what we want to go in here which will 51 04:26.040 --> 04:31.620 allow the user to determine which fields they want to update and which fields they don't want to update. 52 04:31.620 --> 04:39.290 So all we have to do inside here is to specify the req.body. 53 04:39.420 --> 04:46.560 And now when a client sends over a PATCH request then body-parser will repass the request and it will 54 04:46.560 --> 04:49.570 pick out the fields that they have provided updates to 55 04:49.710 --> 04:56.790 and by using the set flag we're able to update our database for only the fields that have a new value 56 04:57.090 --> 04:59.820 which is fulfilling our PATCH request's duty. 57 05:00.780 --> 05:04.230 So now that we've completed this we're almost ready to test it. 58 05:07.410 --> 05:15.120 All we have to do now is to simply add in our callback, so our function that can send an error. 59 05:15.450 --> 05:26.460 And if there were no errors then we will simply res.send "Successfully updated article." 60 05:26.940 --> 05:31.860 But if there were errors then we will res.send the errors. 61 05:31.860 --> 05:38.460 So now let's go and close off all of the places where we need semicolons, hit save. 62 05:38.490 --> 05:44.370 Now before we test it I just want to show you that I've updated our database so that we restore our 63 05:44.370 --> 05:46.230 database to the previous values. 64 05:46.230 --> 05:51.840 So my article on Jack Bauer now has the previous title and the previous content and I did this by simply 65 05:51.840 --> 05:58.590 right clicking on my article entry and just editing the document by copy and pasting the values that 66 05:58.590 --> 06:02.730 we had from the GitHub repopulate database page. 67 06:02.730 --> 06:11.160 So, now that I have the original title and the original content it's time to give patch a spin using 68 06:11.190 --> 06:11.890 Postman. 69 06:12.000 --> 06:13.970 So I'm going to change my request to PATCH. 70 06:14.010 --> 06:20.310 I'm going to leave my URL exactly the same targeting articles/Jack%20Bauer in my body this time 71 06:20.700 --> 06:23.800 I am only going to change the title. 72 06:23.880 --> 06:28.680 So if you have any key and value pairs left over from previously you can just click on the cross to 73 06:28.680 --> 06:30.810 delete them so you get a fresh start. 74 06:31.140 --> 06:37.980 But in my case the only property I want to update is the title and I'm gonna change that to just say 75 06:38.010 --> 06:39.280 Chuck Norris. 76 06:39.290 --> 06:46.710 So, all going well this should update my article on Jack Bauer to change the title to Chuck Norris believe 77 06:46.740 --> 06:50.220 the content and everything else completely intact. 78 06:50.220 --> 06:55.860 So let's go ahead and hit send and we get a successful message back 79 06:55.860 --> 07:02.700 but the moment of truth is when we update our articles collection and when we take a look at our last 80 07:02.700 --> 07:08.400 entry, you can see that the content, the id, everything else has been kept exactly the same 81 07:08.400 --> 07:15.900 and the only change is the one where I provided a value for. And that is how the PATCH method should 82 07:15.900 --> 07:16.320 work.