1 00:00:00,000 --> 00:00:02,899 Let's refactor our other GET route in a similar way. 2 00:00:02,899 --> 00:00:06,522 We know that we want this route to respond with a specific quote. 3 00:00:06,522 --> 00:00:10,459 The client will send us a request containing the unique ID number of 4 00:00:10,459 --> 00:00:11,807 the requested quote. 5 00:00:11,807 --> 00:00:15,583 We'll use that number to find the correct quote in our data store, and 6 00:00:15,583 --> 00:00:18,144 then we'll send that quote back to the client. 7 00:00:18,144 --> 00:00:22,780 Make sure that you've deleted the code inside of this route that returns a single 8 00:00:22,780 --> 00:00:25,039 quote, and type records.getQuote. 9 00:00:27,967 --> 00:00:32,560 If we hover over getQuote, we can see that it takes one parameter, an ID. 10 00:00:32,560 --> 00:00:35,680 Remember that each quote contains a unique ID number. 11 00:00:35,680 --> 00:00:40,443 This function takes that unique ID number, finds the right quote and returns it so 12 00:00:40,443 --> 00:00:43,079 that our API can then return it to the client. 13 00:00:43,079 --> 00:00:46,762 An ORM, like Sequelize or Mongoose, would do something similar. 14 00:00:46,762 --> 00:00:51,306 Provide some kind of function that can find an item in a database by its unique 15 00:00:51,306 --> 00:00:52,024 ID number. 16 00:00:52,024 --> 00:00:55,689 First, we'll need to know the ID of the quote the client is requesting. 17 00:00:55,689 --> 00:00:58,952 We can do that in a very similar way as we've already done. 18 00:00:58,952 --> 00:01:03,800 The client can send the ID and we can then pass the ID to the getQuote function. 19 00:01:03,800 --> 00:01:06,169 How do we pass the ID to the function? 20 00:01:06,169 --> 00:01:10,505 Just as we did before, we can access it through req.params.id. 21 00:01:13,368 --> 00:01:17,639 And we can store the result in a variable called quote. 22 00:01:17,639 --> 00:01:20,456 Then we can send that quote back to the client as JSON. 23 00:01:24,249 --> 00:01:26,388 We need to make sure that JavaScript waits for 24 00:01:26,388 --> 00:01:30,810 records.getQuote to return the data before we send the response back to the client. 25 00:01:30,810 --> 00:01:32,980 We'll do that with async and await. 26 00:01:32,980 --> 00:01:36,470 If you want, pause the video, look at the other GET route we've already written, and 27 00:01:36,470 --> 00:01:39,410 see if you can sus out how to use async await here. 28 00:01:39,410 --> 00:01:41,457 Remember, if I'm going to use await, 29 00:01:41,457 --> 00:01:43,760 I need to use it inside of an async function. 30 00:01:43,760 --> 00:01:48,588 So I'll mark this anonymous callback function as an asynchronous function. 31 00:01:51,485 --> 00:01:54,948 And then we need to await the retrieval of the quote we want to send back to 32 00:01:54,948 --> 00:01:55,600 the client. 33 00:01:57,290 --> 00:02:00,680 The code we've written here is going to take whatever is passed in 34 00:02:00,680 --> 00:02:03,950 after the /: and feed it to this function, 35 00:02:03,950 --> 00:02:07,230 which in turn finds the correct quote from our data store. 36 00:02:07,230 --> 00:02:12,000 Let's test this out, save and start your server if it's not already started. 37 00:02:12,000 --> 00:02:17,690 Open up the browser and navigate to the application at localhost:3000/quotes. 38 00:02:17,690 --> 00:02:21,111 You can see in the browser, I have my JSON with all my quotes. 39 00:02:21,111 --> 00:02:24,546 Let's request a quote with an ID of 8721. 40 00:02:27,234 --> 00:02:29,130 And everything's working great. 41 00:02:29,130 --> 00:02:32,550 You'll notice that if I request an ID that doesn't exist, 42 00:02:32,550 --> 00:02:36,010 like a jumble of letters, that nothing happens. 43 00:02:36,010 --> 00:02:40,741 We'll talk about how to handle potential errors such as these later in the course. 44 00:02:40,741 --> 00:02:42,626 We've covered a lot in this course. 45 00:02:42,626 --> 00:02:46,247 We've learned how to write GET routes using a data store and module, 46 00:02:46,247 --> 00:02:50,066 as well as a bit about how to handle asynchronous JavaScript in our code. 47 00:02:50,066 --> 00:02:53,315 Now it's full steam ahead to build the rest of our application. 48 00:02:53,315 --> 00:02:56,255 Routes to add, edit, and delete quotes.