1 00:00:00,180 --> 00:00:06,390 NOW THAT YOU HAVE THE REST API and points for creating resources we're going to focus on the rest api 2 00:00:06,390 --> 00:00:08,830 endpoints for reading resources. 3 00:00:08,910 --> 00:00:13,570 As we explored earlier in the little visualization there are gonna be two. 4 00:00:13,620 --> 00:00:20,070 One that allows us to get back a list of multiple items and one that allows us to target a specific 5 00:00:20,130 --> 00:00:23,250 item by its unique identifier. 6 00:00:23,250 --> 00:00:28,890 Let's go ahead and kick things off with the two routes for users in this video. 7 00:00:28,890 --> 00:00:31,310 That is your challenge for the next video. 8 00:00:31,380 --> 00:00:34,560 You'll be doing the same thing for tasks down below. 9 00:00:34,560 --> 00:00:39,620 Now let's get started by setting up the root handler for fetching multiple users app. 10 00:00:39,640 --> 00:00:44,970 Get to set up the GET request then the path forward slash users. 11 00:00:44,970 --> 00:00:51,570 After that we have the callback function where we get access to the request and to the response from 12 00:00:51,570 --> 00:00:51,930 here. 13 00:00:51,930 --> 00:00:57,200 We're going to go ahead and actually use Mongoose to fetch all of the users from the database. 14 00:00:57,240 --> 00:01:03,620 Now Mongoose provides us with a series of methods similar to those we saw with the Mongo D.B. native 15 00:01:03,630 --> 00:01:04,550 driver. 16 00:01:04,620 --> 00:01:11,100 Let's take a quick moment to explore where we can find these before we use our first one over in Google 17 00:01:11,100 --> 00:01:11,560 Chrome. 18 00:01:11,820 --> 00:01:18,420 I'll be navigating over to my Mongoose tab and then the documentation under guides we're looking for 19 00:01:18,420 --> 00:01:20,090 these queries guide. 20 00:01:20,190 --> 00:01:27,060 This contains a list of all of the methods we have access to directly on the model so directly on uppercase 21 00:01:27,120 --> 00:01:30,540 U user or uppercase t task. 22 00:01:30,660 --> 00:01:36,440 Here I have methods for some of the other CRUD operations we already know how to create right here. 23 00:01:36,450 --> 00:01:43,960 I have find methods for the reading operations I have to update methods for updating one or many. 24 00:01:44,040 --> 00:01:50,850 And I have methods for deleting one or many documents so we'll be using these in order to achieve our 25 00:01:50,850 --> 00:01:51,670 goal. 26 00:01:51,690 --> 00:01:58,530 Now when it comes to fetching multiple we use the find method and the find method accepts a query an 27 00:01:58,560 --> 00:02:01,690 object just like we saw with the native driver. 28 00:02:01,690 --> 00:02:03,420 So over here what am I going to do. 29 00:02:03,690 --> 00:02:08,570 Well for users what I'll be doing is calling user dot. 30 00:02:08,610 --> 00:02:09,260 Fine. 31 00:02:09,300 --> 00:02:15,260 Now find accepts as its first argument an object where we could refine our search criteria. 32 00:02:15,270 --> 00:02:21,960 For example we could try to find users where name equals Andrew similar to what we did with the Mongo 33 00:02:21,980 --> 00:02:22,510 DB. 34 00:02:22,560 --> 00:02:26,370 Native driver in this case we don't want to do any searching. 35 00:02:26,430 --> 00:02:29,500 So we're gonna go ahead and leave it as an empty object. 36 00:02:29,520 --> 00:02:33,600 This is going to fetch all users stored in the database. 37 00:02:33,600 --> 00:02:40,530 Now we get access to those via the promise that find returns the promise will be fulfilled with the 38 00:02:40,530 --> 00:02:41,130 users. 39 00:02:41,130 --> 00:02:47,490 If things go well or it'll be rejected with an error if things go wrong right here that means we set 40 00:02:47,490 --> 00:02:52,820 up a call to dot then and we go ahead and provide our callback function. 41 00:02:52,860 --> 00:02:59,790 If this function runs that means things went well and we'll have access to the users right here users 42 00:03:00,180 --> 00:03:03,210 if things go poorly we will set up the catch call. 43 00:03:03,240 --> 00:03:09,170 This is going to get access to the error and we'll be able to go ahead and respond appropriately. 44 00:03:09,210 --> 00:03:14,250 Let's go ahead and talk about the success case if we are able to query for the users. 45 00:03:14,250 --> 00:03:15,560 What do we want to do. 46 00:03:15,570 --> 00:03:17,970 Well we want to go ahead and send them back. 47 00:03:18,180 --> 00:03:25,740 So what I'll do is use response dot send to provide that array of users as the response. 48 00:03:25,770 --> 00:03:31,560 Now before we worry about handling any errors let's go ahead and make sure this is at least working. 49 00:03:31,560 --> 00:03:37,920 What I'm going to do is head over to postmen and we're going to create a new request in that task app 50 00:03:37,920 --> 00:03:39,870 collection right here. 51 00:03:39,870 --> 00:03:41,960 Let's go ahead and add a request. 52 00:03:42,030 --> 00:03:43,820 I'll pick a name for it. 53 00:03:43,890 --> 00:03:48,520 I can call this one something like read users. 54 00:03:48,720 --> 00:03:52,850 I will save it and we'll go ahead and set up the details by clicking on it. 55 00:03:52,860 --> 00:03:57,740 So right here we have read to users and we're going to leave this as a get request. 56 00:03:57,750 --> 00:04:05,640 Then we'll provide the U.R.L. that would be local host port three thousand forward slash users. 57 00:04:05,640 --> 00:04:12,540 Now we already have a root handler for forward slash users but that was a post request that is completely 58 00:04:12,540 --> 00:04:16,450 different from the one we're using here with the get request. 59 00:04:16,470 --> 00:04:22,050 Now if I go ahead and fire this off I would expect to see an array of users coming back and that is 60 00:04:22,050 --> 00:04:24,200 exactly what I have right here. 61 00:04:24,200 --> 00:04:30,750 I have all of my users showing up as expected I have my Andrew's and my one Andrew meet. 62 00:04:31,110 --> 00:04:37,550 Now one thing we want to make sure we do as we build out our various requests is save them up above 63 00:04:37,560 --> 00:04:42,870 I can see that all of them have this little circle meaning that changes have been made to the request 64 00:04:42,900 --> 00:04:44,550 that haven't been saved. 65 00:04:44,610 --> 00:04:47,370 Right here we can click the save button for each. 66 00:04:47,430 --> 00:04:49,950 Or we can use command s on a Mac. 67 00:04:49,950 --> 00:04:53,540 That would be control s on other operating systems. 68 00:04:53,610 --> 00:05:00,190 So make sure you do save your requests so we can easily access them in the future and we run them actually 69 00:05:00,340 --> 00:05:01,820 as we ran them before. 70 00:05:01,840 --> 00:05:07,690 Now back in Visual Studio code we can make sure that we do do something when we can't connect to the 71 00:05:07,720 --> 00:05:10,210 database and actually get the users. 72 00:05:10,210 --> 00:05:17,070 In this case I'll be using response dot status to set up a five one hundred status code right here. 73 00:05:17,080 --> 00:05:20,440 Five hundred which is an internal server error. 74 00:05:20,470 --> 00:05:26,510 So in this case we would see this run if something like the database connection was lost right here. 75 00:05:26,710 --> 00:05:31,060 Well then use dot send and we actually don't need to send anything back. 76 00:05:31,120 --> 00:05:36,490 We can just send back the response with the status code letting them know that the service is currently 77 00:05:36,490 --> 00:05:37,500 down. 78 00:05:37,510 --> 00:05:40,300 Now let's move on and to these second get handler. 79 00:05:40,300 --> 00:05:42,550 We're going to set up for users. 80 00:05:42,580 --> 00:05:49,930 This one is going to allow us to fetch an individual user by I.D. so we'll still be using app dot get 81 00:05:50,140 --> 00:05:53,010 but the path is going to look a bit different path. 82 00:05:53,020 --> 00:05:53,680 We'll start off. 83 00:05:53,680 --> 00:05:58,540 Much the same way it'll start off with forward slash users and forward slash. 84 00:05:58,540 --> 00:06:04,270 But then what comes after that forward slash is going to be dynamic it's going to be the idea of the 85 00:06:04,270 --> 00:06:10,300 particular user you're trying to fetch and that's obviously going to change as you make requests to 86 00:06:10,300 --> 00:06:14,550 this end point one time it might be something like a e. 87 00:06:14,650 --> 00:06:17,200 Another time the I.D. might be twelve. 88 00:06:17,230 --> 00:06:23,680 So what we want to do is capture whatever value is put right here after this second forward slash and 89 00:06:23,680 --> 00:06:28,480 get access to it in the root handler so we can fetch the user correctly. 90 00:06:28,630 --> 00:06:33,230 To do that express it gives us access to something called Route parameters. 91 00:06:33,340 --> 00:06:38,160 These are parts of the you are that are used to capture dynamic values. 92 00:06:38,170 --> 00:06:44,680 And it looks a little bit like this after the forward slash we add a colon followed by a name we can 93 00:06:44,680 --> 00:06:46,150 pick whatever name we like. 94 00:06:46,150 --> 00:06:47,680 I'm going to use I.D.. 95 00:06:47,860 --> 00:06:54,100 So essentially we're saying someone's going to make a get request to forward slash users forward slash 96 00:06:54,130 --> 00:06:56,010 something when that happens. 97 00:06:56,020 --> 00:07:02,110 I want this root handler to run the one I'm setting up right now and I want to be able to access the 98 00:07:02,110 --> 00:07:05,150 dynamic value they provided inside of here. 99 00:07:05,230 --> 00:07:07,760 And that's exactly what we're gonna be able to do. 100 00:07:07,810 --> 00:07:12,750 Request gives us access to request dot parents. 101 00:07:12,790 --> 00:07:19,510 This contains all of the route parameters that we're provided in this case it is an object with a single 102 00:07:19,510 --> 00:07:26,860 property I.D. and the value for I.D. is whatever was put inside of the U.R.L. let's mess around with 103 00:07:26,860 --> 00:07:27,220 this. 104 00:07:27,220 --> 00:07:34,060 What I'm gonna do is ramp the request dot parameter in a console dot log call so I can print those out 105 00:07:34,360 --> 00:07:36,900 and I'm not even going to send back a response. 106 00:07:36,940 --> 00:07:42,110 Now obviously that's not what we want in the long term but for this little demonstration it's OK if 107 00:07:42,110 --> 00:07:46,140 you request times out because no response is ever set. 108 00:07:46,210 --> 00:07:52,360 Let's save the file to restart the server and test things out from postmen right here. 109 00:07:52,360 --> 00:07:59,750 I'll be adding a new request and we can call this something like read user singular. 110 00:07:59,890 --> 00:08:03,640 I'm going to go ahead and save that and we'll set up the correct you are. 111 00:08:03,850 --> 00:08:09,030 I'll click the request in the collections list over here that is local host colon. 112 00:08:09,040 --> 00:08:14,460 Three thousand forward slash users forward slash some value. 113 00:08:14,470 --> 00:08:17,020 This could be anything as an example. 114 00:08:17,020 --> 00:08:20,380 Let's just enter a bunch of random numbers inside of here. 115 00:08:20,380 --> 00:08:22,920 You don't have to match up with mine exactly. 116 00:08:22,960 --> 00:08:24,510 We're going to click send. 117 00:08:24,610 --> 00:08:30,000 And down below we're gonna see the loading screen forever because we never send back a response. 118 00:08:30,130 --> 00:08:37,690 But over inside a visual studio code down below I can see what's stored on that RAM's object right here. 119 00:08:37,780 --> 00:08:42,750 I have I.D. and it is the idea value I typed as part of the U.R.L.. 120 00:08:43,150 --> 00:08:49,000 So now that we know how we can extract that value we'll be able to actually fetch the user if any and 121 00:08:49,000 --> 00:08:51,150 get that sent back right here. 122 00:08:51,280 --> 00:08:53,580 Let's go ahead and start that process. 123 00:08:53,620 --> 00:08:55,550 I'm going to create a new concept. 124 00:08:55,750 --> 00:09:03,420 I'm going to call this underscore I.D. and then I'm going to get its value from request dot per Ms. 125 00:09:03,520 --> 00:09:09,760 Dot right here I.D. which matches up with what we had up above and what we were seeing when we logged 126 00:09:09,760 --> 00:09:12,590 out the parameter object down below. 127 00:09:12,640 --> 00:09:18,490 The next thing we want to do is use another one of those query methods to actually fetch the user over 128 00:09:18,490 --> 00:09:20,260 in the Mongoose documentation. 129 00:09:20,260 --> 00:09:23,790 There are two main ways we can fetch a single item. 130 00:09:23,890 --> 00:09:31,510 We have find one and we have a find by I.D. so I could use find one if I'm trying to find a user buy 131 00:09:31,510 --> 00:09:38,320 something else like maybe their email address I can use find by I.D. if I'm trying to find them by their 132 00:09:38,320 --> 00:09:41,440 I.D. which is exactly what we're trying to do here. 133 00:09:41,800 --> 00:09:48,040 So with find one we provide an object with our search criteria with find my I.D. We just provide the 134 00:09:48,040 --> 00:09:50,160 I.D. right here for us. 135 00:09:50,290 --> 00:09:58,430 That would be user dot find by I.D. and we provide the I.D. which we just grabbed up above. 136 00:09:58,570 --> 00:10:05,380 Now we can set up our promise methods we have our then call if we did indeed get the user back right 137 00:10:05,380 --> 00:10:12,820 here we're gonna have access to that user as the argument and then down below we'll also set up a call 138 00:10:12,820 --> 00:10:17,570 to catch if we were not able to complete that query successfully. 139 00:10:17,620 --> 00:10:21,640 And right here if that's the case we'll have access to the error. 140 00:10:21,640 --> 00:10:28,110 Now what exactly do we want to do inside of our first callback function when the promise is fulfilled. 141 00:10:28,120 --> 00:10:34,510 It's important to remember once again that a mongo DV query is not considered a failure if we don't 142 00:10:34,510 --> 00:10:39,080 get any results back when we're looking for something that's considered a success. 143 00:10:39,160 --> 00:10:45,820 Mongo DB was able to find everything you asked for and get it back to you correctly even if it was nothing 144 00:10:45,850 --> 00:10:47,770 because there was no matches. 145 00:10:47,830 --> 00:10:52,890 That means this first function is going to run but we might not always have a user. 146 00:10:52,930 --> 00:10:58,930 There's a chance that we weren't able to find a user with that I.D. In which case we would have no value 147 00:10:58,930 --> 00:10:59,590 here. 148 00:10:59,710 --> 00:11:04,500 So we'll use a little bit of conditional logic to once again keep things nice and organized. 149 00:11:04,540 --> 00:11:11,890 If there is no user we'll go ahead and run some code I'm using return to stop the function execution 150 00:11:12,100 --> 00:11:16,770 though I could always use an else clause to do something similar and right here. 151 00:11:16,780 --> 00:11:18,030 What are we going to do. 152 00:11:18,040 --> 00:11:22,930 We're going to send back a forum for saying the user was not found. 153 00:11:22,930 --> 00:11:30,150 So that would be response dot status sending an equal to 4 0 for then dot send. 154 00:11:30,160 --> 00:11:31,240 Perfect. 155 00:11:31,240 --> 00:11:37,570 Now if our actual catch function runs we'll do something similar to what we did up above response dot 156 00:11:37,570 --> 00:11:38,160 status. 157 00:11:38,170 --> 00:11:41,860 Five hundred and using send to send that back. 158 00:11:41,980 --> 00:11:47,770 Now the only thing left to do is send back the response when things go well and that's going to happen 159 00:11:47,830 --> 00:11:52,050 right here in this case we want to make sure to send that data back. 160 00:11:52,060 --> 00:11:59,120 So right here what I'll do is use response dot send to send back the individual user. 161 00:11:59,170 --> 00:12:04,900 Now in this case I'm not explicitly changing the status code which means the status will be two hundred 162 00:12:05,050 --> 00:12:10,630 and that's perfectly fine for our endpoints that involve fetching a resource. 163 00:12:10,630 --> 00:12:14,950 Now let's go ahead and actually test this out over inside of postmen. 164 00:12:14,950 --> 00:12:18,790 We're going to fire off the exact same request we had before. 165 00:12:18,790 --> 00:12:24,350 This is gonna be seen as the I.D. and we're not going to find any record with that I.D.. 166 00:12:24,460 --> 00:12:28,120 And down below we get a 4 0 4 as expected. 167 00:12:28,120 --> 00:12:34,600 Now let's use a valid I.D. and see what we get back for that over in my read user's request I have a 168 00:12:34,600 --> 00:12:40,750 few different ideas showing up I'm going to go ahead and grab just one of them then I'll go over to 169 00:12:40,780 --> 00:12:48,040 read user I'll swap out that portion of the U.R.L. with the I.D. I just copied it to the clipboard. 170 00:12:48,130 --> 00:12:54,370 I'm gonna fire that off and this time around I get a status code of two hundred and I actually get my 171 00:12:54,370 --> 00:12:57,900 user object down below which is fantastic. 172 00:12:57,910 --> 00:13:05,230 So now we have our resource creation endpoints for fetching all items or fetching a single item by its 173 00:13:05,230 --> 00:13:05,970 I.D.. 174 00:13:05,980 --> 00:13:11,800 Now one thing you'll notice about the code down below is that I never convert my string I.D. into an 175 00:13:11,830 --> 00:13:19,810 object idea like we had to do with the Mongo D.B. native driver when finding documents Mongoose does 176 00:13:19,810 --> 00:13:21,610 many nice things for us. 177 00:13:21,610 --> 00:13:28,030 And one nice thing it does is that it automatically converts those string ideas into object I.D. to 178 00:13:28,030 --> 00:13:30,170 make sure everything works correctly. 179 00:13:30,250 --> 00:13:32,950 So that's not the main reason we're using mongoose. 180 00:13:32,950 --> 00:13:38,230 The main reason is to create those models and structure our data in a nice way. 181 00:13:38,230 --> 00:13:43,810 But this is definitely a nice little advantage now that's what we're going to stop for this one in the 182 00:13:43,810 --> 00:13:44,930 next video. 183 00:13:44,950 --> 00:13:49,380 It's up to you to set up two end points for fetching tasks. 184 00:13:49,420 --> 00:13:50,740 I'm excited to get to that. 185 00:13:50,770 --> 00:13:52,660 So let's jump right into the next one.