1 00:00:00,240 --> 00:00:05,640 In this video you're going to learn how to set up a TTP end points for updating resources. 2 00:00:05,640 --> 00:00:12,360 Together we'll focus on updating a user and in the next video it will be your challenge to update a 3 00:00:12,360 --> 00:00:17,660 task so down below at the end of our three user related end points. 4 00:00:17,670 --> 00:00:20,970 We're gonna go ahead and add a fourth right here. 5 00:00:20,970 --> 00:00:29,070 This will be using app dot patch the patch each TTP method which is designed for updating an existing 6 00:00:29,100 --> 00:00:29,960 resource. 7 00:00:29,970 --> 00:00:34,160 So right here we'll set up the path for this particular endpoint. 8 00:00:34,230 --> 00:00:36,750 That's gonna be the same as what we have above. 9 00:00:37,140 --> 00:00:41,650 So here we are trying to get an individual user by its I.D.. 10 00:00:41,730 --> 00:00:46,340 Down below we are trying to update an individual user by its I.D.. 11 00:00:46,560 --> 00:00:54,390 So that would be forward slash users forward slash colon I.D. then we'll set up our async function. 12 00:00:54,390 --> 00:01:01,770 So right here a sync will set up the two arguments request and response then we'll go ahead and finish 13 00:01:01,770 --> 00:01:04,530 off the syntax for the Arrow function. 14 00:01:04,530 --> 00:01:09,900 Now the first thing we're gonna do inside of here is set up that try catch block and we're going to 15 00:01:09,900 --> 00:01:13,980 attempt to update the user with the provided I.D.. 16 00:01:14,010 --> 00:01:17,010 Let's go ahead and set the try catch block up. 17 00:01:17,010 --> 00:01:23,760 So we tried to do something update a user by its I.D. And if that doesn't go well we will get the error 18 00:01:23,790 --> 00:01:26,190 in the catch block down below. 19 00:01:26,190 --> 00:01:30,990 Now for the catch block what we're gonna do is hold off for the moment and we'll go ahead and start 20 00:01:30,990 --> 00:01:31,880 with try. 21 00:01:32,430 --> 00:01:38,100 Now the Mongoose method we're gonna use to get this done is available in that query is the list we've 22 00:01:38,100 --> 00:01:42,030 been looking at right here up top on the queries guide. 23 00:01:42,030 --> 00:01:45,020 We have find a bi I.D. and update. 24 00:01:45,150 --> 00:01:52,560 Now we have other update methods as well such as find one and update update and update many though we'll 25 00:01:52,560 --> 00:01:58,690 be sticking with this one though technically you can get the job done with any of those four. 26 00:01:58,800 --> 00:02:01,640 Let's go ahead and get started over in sign of the try. 27 00:02:01,640 --> 00:02:07,820 BLOCK Right here what we're gonna get back from find by I.D. and update is going to be the user. 28 00:02:07,830 --> 00:02:16,290 So const user equals we'll go ahead and use await to await the promise that comes back from user dot 29 00:02:16,680 --> 00:02:21,120 find by I.D. and update. 30 00:02:21,120 --> 00:02:21,960 Perfect. 31 00:02:21,960 --> 00:02:27,990 Now the first argument we'll pass in is the idea of the user we're attempting to update and we already 32 00:02:27,990 --> 00:02:33,240 know where we can access that that is request dot parameter dot I.D.. 33 00:02:33,540 --> 00:02:37,900 This is exactly what we did up above for getting the I.D. for the user. 34 00:02:37,920 --> 00:02:44,760 We were trying to read then down below the next argument is the updates we're trying to make now unlike 35 00:02:44,760 --> 00:02:49,390 with the Mongo D.B. native driver there's no need to use anything like these set. 36 00:02:49,390 --> 00:02:52,480 Operator Mongoose handles all of that for us. 37 00:02:52,500 --> 00:02:55,470 Another great reason to actually use the tool. 38 00:02:55,470 --> 00:03:01,900 So right here all we have to do is provide the object with the various fields we want to update. 39 00:03:01,920 --> 00:03:08,150 So if I wanted to update the name I could set a name equal to some new string like Jessica. 40 00:03:08,730 --> 00:03:14,870 Now in this case we don't want to have a static update where we always update the user to the same thing. 41 00:03:14,880 --> 00:03:19,830 What we want to do is take the updates in as the request body. 42 00:03:19,830 --> 00:03:22,050 So much like we did for creating a user. 43 00:03:22,050 --> 00:03:26,510 The data will be passed through via the TTP request. 44 00:03:26,520 --> 00:03:30,290 So right here we are going to access request dot body. 45 00:03:30,300 --> 00:03:31,340 Excellent. 46 00:03:31,350 --> 00:03:38,130 This is exactly what we did up just a little bit in the code when we originally created the user. 47 00:03:38,130 --> 00:03:44,700 Now back down below in the patch route we can go ahead and provide the third argument which is an options 48 00:03:44,760 --> 00:03:45,750 object. 49 00:03:45,750 --> 00:03:50,830 And here we are going to set up a couple of options to get things working the way we want them. 50 00:03:50,880 --> 00:03:55,660 The first option is new and we'll be setting new equal to true. 51 00:03:55,800 --> 00:04:02,820 This is going to return the new user as opposed to the existing one that was found before the update. 52 00:04:03,090 --> 00:04:09,200 So right here we'll have back the latest data the original user with the updates applied. 53 00:04:09,240 --> 00:04:13,730 Next up the second option we're going to set is run validators. 54 00:04:13,770 --> 00:04:17,610 This is going to make sure that we do run validation for the update. 55 00:04:17,610 --> 00:04:24,960 So if I tried to update my name to something non-existent I want to make sure that fails any time we're 56 00:04:24,960 --> 00:04:28,560 allowing the user to write data to the database. 57 00:04:28,560 --> 00:04:33,210 We want to make sure we validate it so that it comes in the format we're expecting. 58 00:04:33,540 --> 00:04:37,260 Now right here there are a couple of different things that could happen. 59 00:04:37,260 --> 00:04:42,020 One the update went well to the update went poorly. 60 00:04:42,210 --> 00:04:48,910 And 3 there was no user to update with that I.D. And we want to make sure that we handle all three. 61 00:04:48,930 --> 00:04:54,010 First up if there was no user with that I.D. There will be no value right here. 62 00:04:54,120 --> 00:04:59,830 So if there is no user what are we going to go ahead and do. 63 00:04:59,830 --> 00:05:02,680 Well we'll be sending back a 4 0 4. 64 00:05:02,710 --> 00:05:10,650 So I'll use return to stop the function execution and I'll use response dot status to set a 4 0 4. 65 00:05:10,660 --> 00:05:14,290 And finally I'll use send to actually send it back. 66 00:05:14,290 --> 00:05:18,940 Now if the code down below right here runs that means everything went well. 67 00:05:19,240 --> 00:05:26,200 I can stick with these two hundred status code which is the default value all I'll do is use response 68 00:05:26,220 --> 00:05:29,860 dot send to send the user data back. 69 00:05:29,890 --> 00:05:36,250 So if someone updates the user we want to send them back the current user data that would be all of 70 00:05:36,250 --> 00:05:39,220 the existing data with their updates applied. 71 00:05:39,220 --> 00:05:42,230 Now the next case is down below in catch. 72 00:05:42,250 --> 00:05:44,890 This is going to run if something goes wrong. 73 00:05:44,890 --> 00:05:47,000 Now this could be one of two things. 74 00:05:47,020 --> 00:05:52,540 It's possible that we have an error like the other ones we've talked about where we want to send back 75 00:05:52,570 --> 00:05:59,260 a five hundred because we weren't able to connect to the database or something along those lines a server 76 00:05:59,290 --> 00:06:00,760 related issue. 77 00:06:00,760 --> 00:06:07,510 It's also possible that it's a validation related issue where someone has tried to update their name 78 00:06:07,510 --> 00:06:13,630 and they've tried to set it equal to an empty string as an example which would not be valid based off 79 00:06:13,630 --> 00:06:16,360 of the model we set up for a user. 80 00:06:16,600 --> 00:06:20,090 For the moment we're not going to worry about handling both cases. 81 00:06:20,200 --> 00:06:23,020 We'll just focus on handling validation. 82 00:06:23,080 --> 00:06:27,450 So that would be response dot status and we'll send back a 400. 83 00:06:27,550 --> 00:06:32,170 This is exactly what we did up above when creating a user went wrong. 84 00:06:32,170 --> 00:06:39,790 Then we'll go ahead and send the error back right here dot send sending back the error object now that 85 00:06:39,790 --> 00:06:43,420 we have this in place we have that route completely set up. 86 00:06:43,420 --> 00:06:47,830 I can save the program and go ahead and test this out from here. 87 00:06:47,830 --> 00:06:52,540 We'll be moving over into postmen to make sure the routes working as expected. 88 00:06:52,540 --> 00:06:59,450 I'm going to go ahead and create a brand new request over here for the task app we'll be setting this 89 00:06:59,450 --> 00:07:04,190 one up with a name like update user once I save it. 90 00:07:04,190 --> 00:07:08,970 The first thing we're gonna do is crack it open and switch up the TTP method. 91 00:07:08,990 --> 00:07:12,200 We are going from get over to patch. 92 00:07:12,200 --> 00:07:14,440 Then we'll provide the U.R.L.. 93 00:07:14,450 --> 00:07:17,960 Now we are going to need the I.D. of someone to update. 94 00:07:18,050 --> 00:07:24,240 I can always use read users to get a list of all of my users back and pick one that I'd like to change. 95 00:07:24,260 --> 00:07:26,490 So right here I have a few different ones. 96 00:07:26,540 --> 00:07:30,210 I'll go ahead and grab the I.D. for this first user. 97 00:07:30,210 --> 00:07:35,180 Now I'll go back over to the update user request and complete the you are out. 98 00:07:35,180 --> 00:07:36,570 That's local host colon. 99 00:07:36,760 --> 00:07:44,530 Three thousand forward slash users forward slash some I.D. Now in order to actually set up the updates 100 00:07:44,540 --> 00:07:52,550 we do want to provide the body so we'll go to Body raw and we'll switch from text over to Jason like 101 00:07:52,550 --> 00:07:56,570 we had done for our to create requests right here. 102 00:07:56,570 --> 00:08:02,510 We'll be able to provide the Jason describing what exactly we would like to change and in our case I'm 103 00:08:02,510 --> 00:08:04,980 gonna add a last name onto the name. 104 00:08:05,300 --> 00:08:07,800 So right here I will be updating name. 105 00:08:07,850 --> 00:08:11,970 I'll go ahead and change it from Andrew over to Andrew Meade. 106 00:08:12,020 --> 00:08:18,170 Now if I go ahead and fire this off I would expect a two hundred status code as the response with my 107 00:08:18,230 --> 00:08:21,580 updated user as the response body down below. 108 00:08:21,680 --> 00:08:23,780 That is exactly what I end up getting. 109 00:08:24,020 --> 00:08:29,090 So right here I am indeed able to update the user as expected. 110 00:08:29,090 --> 00:08:35,510 Now if we were to swap out that I.D. with something that did not exist I would get the 4 0 4 and if 111 00:08:35,510 --> 00:08:42,560 we were to go back to the I.D. that was valid but try to update in an invalid way I would expect to 112 00:08:42,560 --> 00:08:44,130 see that four hundred. 113 00:08:44,180 --> 00:08:50,570 So right here I have the valid I.D. once again and I'll try to set the name equal to nothing an empty 114 00:08:50,570 --> 00:08:51,330 string. 115 00:08:51,530 --> 00:08:58,280 When I do that down below I get a four hundred bad request with my validation error showing up. 116 00:08:58,280 --> 00:09:02,480 So here we can see that we are indeed able to update the user as expected. 117 00:09:02,480 --> 00:09:08,380 Now if we tried to update a property that the user didn't have it would be completely ignored. 118 00:09:08,390 --> 00:09:14,240 So right here the only thing I'm gonna try to update is height now height is not something we track 119 00:09:14,240 --> 00:09:17,680 for users though I'll try to set it equal to 72. 120 00:09:17,720 --> 00:09:22,600 That would be my height in inches if I go ahead and send this off down below. 121 00:09:22,670 --> 00:09:29,060 We're gonna see our user object but we're not going to see a height property so any of those properties 122 00:09:29,060 --> 00:09:32,420 that don't exist on the user will be completely ignored. 123 00:09:32,450 --> 00:09:34,670 Now you'll notice we're getting a two hundred. 124 00:09:34,670 --> 00:09:38,020 That's because once again mongoose is ignoring them. 125 00:09:38,090 --> 00:09:44,240 If you wanted to set up an error response you would have to add some custom code to get that done and 126 00:09:44,240 --> 00:09:46,430 I'll show you how to do that right now. 127 00:09:46,430 --> 00:09:53,510 So let's say we want to send back a 400 when you tried to update a property that's not something you 128 00:09:53,510 --> 00:09:55,070 can actually change. 129 00:09:55,100 --> 00:10:00,710 This would be something like underscore I.D. which you can't update or something like height which doesn't 130 00:10:00,710 --> 00:10:03,550 exist in the first place to get this done. 131 00:10:03,560 --> 00:10:08,200 The first thing we want to do is figure out what someone should be allowed to update. 132 00:10:08,390 --> 00:10:14,990 And we're going to store this in and allowed updates array right here we can list out the individual 133 00:10:14,990 --> 00:10:18,020 properties we would like someone to be able to change. 134 00:10:18,050 --> 00:10:26,340 This is going to be an array of strings so I'll allow the updating of name email password. 135 00:10:26,350 --> 00:10:27,870 And finally H. 136 00:10:27,910 --> 00:10:30,690 Everything else will be considered invalid. 137 00:10:30,700 --> 00:10:35,060 Next up we have to figure out what they're trying to update with this operation. 138 00:10:35,080 --> 00:10:39,550 Now we know that request not Bonnie is an object with all of those updates. 139 00:10:39,550 --> 00:10:44,050 But what I want back is an array of strings similar to what I have here. 140 00:10:44,200 --> 00:10:48,550 And I'd like to store on a another variable which I'll call updates. 141 00:10:48,550 --> 00:10:55,660 Now to get this done to convert our object into an array of its properties we'll be using object dot 142 00:10:55,780 --> 00:11:00,150 Keats right here we pass in the object we're trying to work with. 143 00:11:00,250 --> 00:11:07,090 So it's going to take this object in and keys will return an array of strings where each is a property 144 00:11:07,120 --> 00:11:08,620 on that object. 145 00:11:08,620 --> 00:11:14,950 Now if I was to use this example data I would have an array with a single item inside the string height 146 00:11:14,980 --> 00:11:18,180 as that's the only property we're trying to update. 147 00:11:18,220 --> 00:11:24,530 Now the next thing we want to determine is if every single update can be found in allowed updates. 148 00:11:24,760 --> 00:11:26,320 If almost all of them are. 149 00:11:26,350 --> 00:11:27,310 That's not enough. 150 00:11:27,310 --> 00:11:30,610 Every single one needs to exist inside of here. 151 00:11:30,610 --> 00:11:36,190 If we're trying to update something that doesn't exist we'll consider the whole operation a failure. 152 00:11:36,440 --> 00:11:43,930 Now to track this we can use a boolean is valid operation or something similarly named and what we're 153 00:11:43,930 --> 00:11:46,240 going to do is use an Array method. 154 00:11:46,240 --> 00:11:48,100 You might not have used before. 155 00:11:48,250 --> 00:11:55,840 This is every now we're gonna use it on updates so it will be updates dot every which we'll call as 156 00:11:55,840 --> 00:11:56,590 a function. 157 00:11:56,590 --> 00:12:02,800 Now every like most of our array methods it takes a callback function as its one and only argument and 158 00:12:02,800 --> 00:12:06,340 this callback function gets called for every item in the array. 159 00:12:06,340 --> 00:12:11,470 So for every attempted update we get access to the individual item right here. 160 00:12:11,500 --> 00:12:17,140 So if I'm trying to update five properties this function would get called five times with those five 161 00:12:17,140 --> 00:12:19,020 different property names. 162 00:12:19,030 --> 00:12:25,240 Now it's up to us to return true if the update is allowed and false otherwise. 163 00:12:25,360 --> 00:12:28,980 So every is going to run your function for every item in the array. 164 00:12:29,260 --> 00:12:33,940 If you always get true as the return value every will return true. 165 00:12:34,090 --> 00:12:37,930 If you don't always get true back every will return false. 166 00:12:38,230 --> 00:12:45,550 So if I have 10 truths then every will return true if I have nine truths and a single false every will 167 00:12:45,550 --> 00:12:47,050 return false. 168 00:12:47,050 --> 00:12:53,590 Now this is exactly what we want we're going to return and we're going to see if the update is included 169 00:12:53,620 --> 00:13:00,880 in allowed updates so allowed updates dot includes which returns a boolean true if it's included and 170 00:13:00,880 --> 00:13:07,690 false otherwise and we'll just check if that individual update is found right here we have exactly what 171 00:13:07,690 --> 00:13:08,820 we would need. 172 00:13:08,830 --> 00:13:11,190 Now we can use the arrow function shorthand. 173 00:13:11,200 --> 00:13:14,080 So here we are just returning the following. 174 00:13:14,200 --> 00:13:21,070 I can take that I can remove the curly braces and I can paste it right here making sure to have the 175 00:13:21,100 --> 00:13:26,500 closing parentheses which matches up with the opening one for the every call. 176 00:13:26,500 --> 00:13:29,130 Now all we have to do is see what is valid. 177 00:13:29,140 --> 00:13:30,610 Operation exists. 178 00:13:30,610 --> 00:13:31,150 Excuse me. 179 00:13:31,150 --> 00:13:32,840 What equals two. 180 00:13:32,860 --> 00:13:36,510 So right here if we want to check if it's false. 181 00:13:36,580 --> 00:13:42,410 So if it is not a valid operation we're gonna go ahead and stop things by using return. 182 00:13:42,640 --> 00:13:49,930 I'll use response dot status to send back a four hundred and then I can use send to send back an object 183 00:13:50,230 --> 00:13:53,320 and we will set up an error property right here. 184 00:13:53,440 --> 00:13:58,230 I could provide an error like the following invalid updates. 185 00:13:58,240 --> 00:13:59,560 Excellent. 186 00:13:59,560 --> 00:14:01,990 Next up what is the actual condition. 187 00:14:01,990 --> 00:14:04,270 Well I want to run this code if is valid. 188 00:14:04,270 --> 00:14:09,520 Operation is false so is valid operation. 189 00:14:09,520 --> 00:14:13,510 Now this is only going to run if it's true in order to reverse that. 190 00:14:13,510 --> 00:14:20,350 We use the logical not operator to flip the boolean from either true to false or false to true. 191 00:14:20,470 --> 00:14:26,800 So right here we have the exact same update logic but with a little additional error handling making 192 00:14:26,800 --> 00:14:30,090 sure that the user is using the operation correctly. 193 00:14:30,220 --> 00:14:36,310 As mentioned it's not going to impact the functionality you already weren't able to update properties 194 00:14:36,310 --> 00:14:37,630 that didn't exist. 195 00:14:37,630 --> 00:14:43,720 The goal here though is to provide the user with as much information as possible as to why things aren't 196 00:14:43,720 --> 00:14:45,230 going as expected. 197 00:14:45,250 --> 00:14:51,610 If I tried to update the height to 72 and I got a success status code but I didn't see it down below 198 00:14:51,610 --> 00:14:53,580 I could be a little confused. 199 00:14:53,710 --> 00:15:00,640 But now if I go ahead and fire off the same operation I get a four hundred with invalid updates showing 200 00:15:00,640 --> 00:15:02,650 up which is fantastic. 201 00:15:02,650 --> 00:15:08,380 Now once again let's go ahead and try to update something valid and make sure that still works. 202 00:15:08,380 --> 00:15:13,870 Right here I'll try to change the name one more time and I'll go ahead and change it to jets. 203 00:15:13,930 --> 00:15:20,470 If I fire that off I do get a two hundred and I can see that property updated down below. 204 00:15:20,470 --> 00:15:21,530 So there we go. 205 00:15:21,550 --> 00:15:27,040 That's how we'll be setting up HDTV routes for up dating a given resource. 206 00:15:27,040 --> 00:15:31,870 Now some of our end points are going to have logic that's a little more complex than others and that's 207 00:15:32,020 --> 00:15:35,870 OK because some are indeed simpler than others. 208 00:15:35,890 --> 00:15:41,110 In general the routes for updating resources are the most complex. 209 00:15:41,110 --> 00:15:47,860 So here we can see we do have more code in a single express route handler than we've had before. 210 00:15:48,070 --> 00:15:53,500 Now that we've done this once together with users in the next video it's going to be your challenge 211 00:15:53,500 --> 00:15:56,200 to do something similar for tasks. 212 00:15:56,230 --> 00:15:57,860 I'm excited to get to that. 213 00:15:57,940 --> 00:16:00,090 So let's go ahead and jump right in.