1 00:00:00,180 --> 00:00:00,960 Welcome back. 2 00:00:00,960 --> 00:00:07,710 In this video as promised it's up to you to complete a challenge using a promise chaining so down below 3 00:00:07,710 --> 00:00:11,580 I have the challenge comments outlining what I'd like you to do. 4 00:00:11,580 --> 00:00:17,580 And the goal is to just mess around with promise chaining you're going to create a new file promise 5 00:00:17,610 --> 00:00:24,300 chaining to that can live alongside of promise chaining and do go ahead and create the second file. 6 00:00:24,360 --> 00:00:29,550 It won't make much of a difference in this particular video but it will have an impact on the next couple 7 00:00:29,550 --> 00:00:30,650 of lectures. 8 00:00:30,660 --> 00:00:33,830 We want a separate file for those two examples. 9 00:00:33,840 --> 00:00:40,620 Next up you want to load in mongoose and the task model so they can be used similar to what we did up 10 00:00:40,620 --> 00:00:44,860 here and then it comes to the two asynchronous things you're gonna do. 11 00:00:44,970 --> 00:00:52,140 The first thing you want to do is remove a given task by its I.D. And then after that you want to figure 12 00:00:52,140 --> 00:00:55,740 out how many incomplete tasks you still have left. 13 00:00:55,740 --> 00:01:01,550 So you want to count up the total number of documents where completed is false. 14 00:01:01,560 --> 00:01:05,780 Once you have all of that in place you want to make sure that you do print that count. 15 00:01:05,820 --> 00:01:08,370 And finally you want to test your work. 16 00:01:08,460 --> 00:01:11,520 Now you already know how to count up the number of documents. 17 00:01:11,520 --> 00:01:13,280 That's something we've done before. 18 00:01:13,440 --> 00:01:16,590 But how do you delete a given task by its I.D.. 19 00:01:17,130 --> 00:01:22,740 Well if we head back over to the Mongoose documentation and go to that queries guide which we've been 20 00:01:22,740 --> 00:01:23,850 to before. 21 00:01:23,850 --> 00:01:26,960 There are a few methods available for getting this done. 22 00:01:27,120 --> 00:01:29,530 Now we have delete many and delete one. 23 00:01:29,640 --> 00:01:38,660 But we also have variations that use find I have find by I.D. and delete and I have fined one and delete. 24 00:01:38,670 --> 00:01:44,160 What I'd like you to do is use fined by I.D. and delete which is really simple to work with. 25 00:01:44,190 --> 00:01:50,040 All you do is pass in the I.D. of the thing you're trying to find and delete. 26 00:01:50,040 --> 00:01:52,430 There are no other arguments required. 27 00:01:52,590 --> 00:01:58,350 So take some time to knock all of that out and test your work make sure your account is indeed updating 28 00:01:58,410 --> 00:02:05,790 when expected then come back and click play. 29 00:02:05,800 --> 00:02:06,580 Welcome back. 30 00:02:06,610 --> 00:02:08,190 Let's go ahead and get to it. 31 00:02:08,200 --> 00:02:13,990 What I'm going to do is create that new file for this challenge right here in the playground directory. 32 00:02:14,080 --> 00:02:23,070 The one that's in task manager I'm going to create Promise hyphen chaining to dot J S. 33 00:02:23,140 --> 00:02:25,810 And here we'll work through that second example. 34 00:02:25,920 --> 00:02:27,430 Now to make things a bit easier. 35 00:02:27,460 --> 00:02:33,340 I'm going to copy the challenge comments from the first file over to the second one so I can actually 36 00:02:33,340 --> 00:02:35,640 see them as I'm working through things. 37 00:02:35,680 --> 00:02:41,800 The first thing we want to do is load in Mongoose and that task model that would be two calls to require 38 00:02:41,860 --> 00:02:50,050 the first dot dot forward slash source forward slash D.B. forward slash Mongoose and we're not actually 39 00:02:50,050 --> 00:02:52,820 getting anything back from that require call. 40 00:02:52,870 --> 00:02:56,590 The second is what we're gonna use to get back the task model. 41 00:02:56,710 --> 00:03:03,060 So I'll create a constant for that right here require dot dot forward slash source. 42 00:03:03,130 --> 00:03:10,180 Then inside of the models directory we have the task script perfect now that we have things in place 43 00:03:10,390 --> 00:03:14,620 we can go ahead and start the process of removing a task by its I.D.. 44 00:03:14,860 --> 00:03:18,640 And to get that done I need an I.D. of one of my tasks. 45 00:03:18,700 --> 00:03:23,630 So over inside of robo three teeth I'll head over to the task collection. 46 00:03:23,650 --> 00:03:26,560 Now I only have one excuse me when I refresh. 47 00:03:26,560 --> 00:03:33,320 I have four and if I head over to the table view we can get an idea of what exactly we have here. 48 00:03:33,330 --> 00:03:35,470 Now none of my tasks are completed. 49 00:03:35,500 --> 00:03:40,650 So currently the count should be four in terms of how many things I still have to do. 50 00:03:40,780 --> 00:03:42,970 I'm going to go ahead and delete this task. 51 00:03:43,000 --> 00:03:49,420 The one that says eat lunch I'll copy the I.D. to the clipboard so I can use it inside of my code. 52 00:03:49,810 --> 00:03:57,300 And over here we're going to use task dot the method I had talked about was fine by I.D.. 53 00:03:57,640 --> 00:04:03,190 And delete this takes in the I.D. of the thing you're trying to remove. 54 00:04:03,190 --> 00:04:06,970 I'll paste that right inside of some quotes as the first argument. 55 00:04:07,090 --> 00:04:09,490 Then we toss on our then call. 56 00:04:09,490 --> 00:04:11,150 There's nothing left to do. 57 00:04:11,230 --> 00:04:13,370 So right here dot that. 58 00:04:13,450 --> 00:04:19,000 Now you didn't need to do anything with the result so you could have actually left the callback like 59 00:04:19,000 --> 00:04:19,530 this. 60 00:04:19,580 --> 00:04:26,110 There's no need to grab it unless we're going to use it optionally maybe you did create a variable for 61 00:04:26,110 --> 00:04:28,870 it like task and dumped it to the terminal. 62 00:04:28,870 --> 00:04:30,130 That would have worked as well. 63 00:04:30,160 --> 00:04:32,200 Either way is perfectly fine. 64 00:04:32,200 --> 00:04:34,850 That wasn't the main point of the challenge. 65 00:04:34,870 --> 00:04:40,960 So once we have the task removed we want to go ahead and get the count so we can actually print it right 66 00:04:40,960 --> 00:04:45,330 here to use promised chaining we'll return the next promise. 67 00:04:45,400 --> 00:04:49,420 That would be task dot count documents. 68 00:04:49,540 --> 00:04:53,200 And remember we provide our criteria for filtering. 69 00:04:53,260 --> 00:04:56,410 In this case we want to find those incomplete tasks. 70 00:04:56,440 --> 00:05:00,980 So those are the ones where completed is equal to False. 71 00:05:01,000 --> 00:05:07,460 Now we can add on these second then call to do something when the count operation is complete. 72 00:05:07,480 --> 00:05:13,090 Right here we're going to get that count and all we're going to do is print it to the terminal console 73 00:05:13,530 --> 00:05:21,010 lug printing the result last up will add a little catch call to handle any potential errors we'll just 74 00:05:21,010 --> 00:05:25,920 get the error and we'll dump it to the console Excellent. 75 00:05:26,130 --> 00:05:31,320 With this in place we're actually all done and now we can go ahead and test things out. 76 00:05:31,320 --> 00:05:35,400 I'm going to try to run this code remove the task and view the count. 77 00:05:35,550 --> 00:05:41,590 Now the current count is four but after I delete it I would expect the count to be three. 78 00:05:41,730 --> 00:05:51,380 I'll use control C to shut things down and then I'll use node playground forward slash promise chaining 79 00:05:52,350 --> 00:05:53,530 to dot J. 80 00:05:53,540 --> 00:05:56,550 S I'm gonna go ahead and run that script. 81 00:05:56,610 --> 00:06:02,870 I can see the document that got deleted and I can see the number three which is fantastic. 82 00:06:02,880 --> 00:06:09,480 Now if I refresh things over inside of robo three teeth I would expect that document to go away and 83 00:06:09,480 --> 00:06:10,310 it does. 84 00:06:10,440 --> 00:06:13,550 And from here look just go ahead and delete one more. 85 00:06:13,620 --> 00:06:19,500 I'll go ahead and delete the one that tells me to finish the node course because I have that twice. 86 00:06:19,500 --> 00:06:22,170 I'll delete the second one right here. 87 00:06:22,170 --> 00:06:24,610 Right click edit document. 88 00:06:24,600 --> 00:06:32,360 I'm gonna grab the I.D. And once again we'll bring that over to visual studio code I'll paste it right 89 00:06:32,360 --> 00:06:33,190 in. 90 00:06:33,380 --> 00:06:35,980 Save the file and we'll rerun that script. 91 00:06:36,110 --> 00:06:39,600 This time I would expect the final count to be two. 92 00:06:39,630 --> 00:06:41,550 I run the program and what do I get. 93 00:06:41,570 --> 00:06:43,400 I get the deleted document. 94 00:06:43,400 --> 00:06:45,900 Then down below I get to. 95 00:06:45,980 --> 00:06:47,020 So there we have it. 96 00:06:47,060 --> 00:06:54,160 A little example using promised chaining to do one thing after something else with our tasks. 97 00:06:54,170 --> 00:06:56,380 That's where we're gonna stop for this video. 98 00:06:56,390 --> 00:07:02,900 Now before we jump back into building our new end points for the task manager API there is one more 99 00:07:02,900 --> 00:07:09,140 important topic I want to cover that is async await and we're gonna start talking about it in the next 100 00:07:09,260 --> 00:07:10,000 lesson. 101 00:07:10,010 --> 00:07:16,040 I am super excited to get to that feature because it really is a game changer when it comes to asynchronous 102 00:07:16,040 --> 00:07:16,950 programming. 103 00:07:17,030 --> 00:07:19,060 So let's go ahead and jump right in.