Welcome back.
In this video as promised it's up to you to complete a challenge using a promise chaining so down below
I have the challenge comments outlining what I'd like you to do.
And the goal is to just mess around with promise chaining you're going to create a new file promise
chaining to that can live alongside of promise chaining and do go ahead and create the second file.
It won't make much of a difference in this particular video but it will have an impact on the next couple
of lectures.
We want a separate file for those two examples.
Next up you want to load in mongoose and the task model so they can be used similar to what we did up
here and then it comes to the two asynchronous things you're gonna do.
The first thing you want to do is remove a given task by its I.D. And then after that you want to figure
out how many incomplete tasks you still have left.
So you want to count up the total number of documents where completed is false.
Once you have all of that in place you want to make sure that you do print that count.
And finally you want to test your work.
Now you already know how to count up the number of documents.
That's something we've done before.
But how do you delete a given task by its I.D..
Well if we head back over to the Mongoose documentation and go to that queries guide which we've been
to before.
There are a few methods available for getting this done.
Now we have delete many and delete one.
But we also have variations that use find I have find by I.D. and delete and I have fined one and delete.
What I'd like you to do is use fined by I.D. and delete which is really simple to work with.
All you do is pass in the I.D. of the thing you're trying to find and delete.
There are no other arguments required.
So take some time to knock all of that out and test your work make sure your account is indeed updating
when expected then come back and click play.
Welcome back.
Let's go ahead and get to it.
What I'm going to do is create that new file for this challenge right here in the playground directory.
The one that's in task manager I'm going to create Promise hyphen chaining to dot J S.
And here we'll work through that second example.
Now to make things a bit easier.
I'm going to copy the challenge comments from the first file over to the second one so I can actually
see them as I'm working through things.
The first thing we want to do is load in Mongoose and that task model that would be two calls to require
the first dot dot forward slash source forward slash D.B. forward slash Mongoose and we're not actually
getting anything back from that require call.
The second is what we're gonna use to get back the task model.
So I'll create a constant for that right here require dot dot forward slash source.
Then inside of the models directory we have the task script perfect now that we have things in place
we can go ahead and start the process of removing a task by its I.D..
And to get that done I need an I.D. of one of my tasks.
So over inside of robo three teeth I'll head over to the task collection.
Now I only have one excuse me when I refresh.
I have four and if I head over to the table view we can get an idea of what exactly we have here.
Now none of my tasks are completed.
So currently the count should be four in terms of how many things I still have to do.
I'm going to go ahead and delete this task.
The one that says eat lunch I'll copy the I.D. to the clipboard so I can use it inside of my code.
And over here we're going to use task dot the method I had talked about was fine by I.D..
And delete this takes in the I.D. of the thing you're trying to remove.
I'll paste that right inside of some quotes as the first argument.
Then we toss on our then call.
There's nothing left to do.
So right here dot that.
Now you didn't need to do anything with the result so you could have actually left the callback like
this.
There's no need to grab it unless we're going to use it optionally maybe you did create a variable for
it like task and dumped it to the terminal.
That would have worked as well.
Either way is perfectly fine.
That wasn't the main point of the challenge.
So once we have the task removed we want to go ahead and get the count so we can actually print it right
here to use promised chaining we'll return the next promise.
That would be task dot count documents.
And remember we provide our criteria for filtering.
In this case we want to find those incomplete tasks.
So those are the ones where completed is equal to False.
Now we can add on these second then call to do something when the count operation is complete.
Right here we're going to get that count and all we're going to do is print it to the terminal console
lug printing the result last up will add a little catch call to handle any potential errors we'll just
get the error and we'll dump it to the console Excellent.
With this in place we're actually all done and now we can go ahead and test things out.
I'm going to try to run this code remove the task and view the count.
Now the current count is four but after I delete it I would expect the count to be three.
I'll use control C to shut things down and then I'll use node playground forward slash promise chaining
to dot J.
S I'm gonna go ahead and run that script.
I can see the document that got deleted and I can see the number three which is fantastic.
Now if I refresh things over inside of robo three teeth I would expect that document to go away and
it does.
And from here look just go ahead and delete one more.
I'll go ahead and delete the one that tells me to finish the node course because I have that twice.
I'll delete the second one right here.
Right click edit document.
I'm gonna grab the I.D. And once again we'll bring that over to visual studio code I'll paste it right
in.
Save the file and we'll rerun that script.
This time I would expect the final count to be two.
I run the program and what do I get.
I get the deleted document.
Then down below I get to.
So there we have it.
A little example using promised chaining to do one thing after something else with our tasks.
That's where we're gonna stop for this video.
Now before we jump back into building our new end points for the task manager API there is one more
important topic I want to cover that is async await and we're gonna start talking about it in the next
lesson.
I am super excited to get to that feature because it really is a game changer when it comes to asynchronous
programming.
So let's go ahead and jump right in.