Hated every one that they shared.
And yes, I know really long journey, but it was really worth it.
Now we have created two method for adding the product and getting all the products.
Now it's time that we see that how many mistakes we have made.
That is, it means we are going to test out this stuff.
So these are the two methods that we have created.
Now it's time to root them up.
So the first ad product is only going to be available for admin.
Of course, only admin can add more products into the application and get all products.
It's going to be available for all the users.
We are going to define a separate method for getting all the products for admin itself.
It's going to be rather easier.
We won't be handling that much of functionality for search and all of that, although we can and we
can still use the same method.
But I would love to create another one.
So let's go into the roots and let's go ahead and see that how we are going to work on with that is
logged in and custom role.
They are really important for us.
So first let's bring in these methods from the controller.
So the first one is add product and the second one is get all products.
Okay, now it's time to define the roots for them.
So first let's go ahead and see that how we want to add the product.
So add the product is going to be slash admin.
Of course, this is important.
And then let's go ahead and call this one as simply product.
And since we are adding this, this makes sense again, feel free to go ahead and change the route as
you like.
And this will be handled by this simple ADD product.
Now, I usually like to keep all of my admins at the bottom, and so I'm going to do the same.
And I'm going to say that, Hey, this is admin roots.
It actually gives a little bit more clarity to the code, what's happening and where it is happening.
And these are just the user roots.
These small comments actually makes a life a little bit easier.
Let's go ahead and make a copy of this and move this guy a little bit up.
There we go.
Okay.
So this is the add route, so admin should be there.
But again, the problem is with the admin route that we need to grab a few information because if you
check up our product controller at the time of adding, we are adding some information which is request
dot user dot ID, that means that user should be logged in and should be admin as well.
So no big deal.
We have already worked on these routes so we can go ahead and do that one more time.
So we're going to say is logged in.
So that will give me all the information and I can also go ahead and kick in another middleware which
is going to be custom role, which works like this.
And we go ahead and say, hey, you are an admin, so only the users will be admin will be able to add
the product.
Now I told you once you add the middleware, it's so simple to work on with the things now the user
route will be able to get all the products.
So this should be rather simpler and we should be able to say just products maybe.
Or you want to change the route.
It's all up to you.
And this one is going to be called a simple get all product.
There we go.
That's it.
Now, first stuff is that we need to add a whole lot of products into our application.
And for this I have actually created some cool stuff for you.
So this is a folder that I'll be sharing with you and it has all the files that you need to upload as
the images.
Of course, our application should look decently beautiful, so that's why we are going to use that.
So inside every single of these there are photos.
So there are coders t shirts, so they are in different colors as well.
Maybe you want to check them out.
For example, there are hoodies here as well.
So these are all the hoodies available.
So nice and easy.
And similar to that there are sweatshirts, so everything is available here and you can go ahead and
use these images into your application.
So there we go.
Come on, stop that.
Okay, so I'll be using these same images.
And again, they are t shirts, full sleeves, t shirts.
These are hoodies, these are sweat, these are regular t shirts, have sleeves and these are pro order,
different variant of instead of write code.
This is pro coder and the same is pro coder t shirt.
So I think these are more than enough for us to actually go ahead and play around with that.
So let's go ahead and first see that if we are able to access this slash admin slash product slash add
route, let's go ahead and copy this.
And we need to use our postman.
So let's go ahead and first create a simple route for adding this.
So this should all actually go away for the test product route and we are going to go ahead and make
a copy of this one and we're going to call this one as simply admin, which is add product.
Let's go ahead and save this one.
This one is obviously going to be a post route and after the post there is going to be a slash admin
slash add product.
Let's go ahead and work on with that.
Let's go ahead and send a request just like anything it says, hey, can not post API slash V1 slash
ad product.
The reason let's go ahead and check if we are actually getting that.
So this one is actually get there should be post.
Save that, and let's go ahead and try it one more time because we're expecting different errors.
So it says error images are required.
Seems like we are already logged in.
So he looks like we are already logged in.
But just to make sure that I'm absolutely sure that everything is all running up and running, let's
go ahead and send a log out request and then try to send an admin and it says cannot read property replace
of undefined.
So again, it was not able to find anything inside the cookies and it was not able to look into the
middleware as well.
So let's go ahead and check the middleware, which is the user.
So inside the middleware, which is user, there we go, is logged in.
So what is happening if it is not able to find this header which is authorized?
So it found nothing inside the cookie token and it didn't find anything inside the header.
So it actually we can actually go ahead and make this token in such a way that if the request or header
have this authorization, then only run the replace method or something.
But again, at least our application is still blocked and it's not allowing me to go further down the
road so we can go ahead and work on with that.
We can modify or make it a little bit more tweaker or better eventually not bother too much.
Right now let's go ahead and log in with two which is admin.
So make sure you are logged in with the admin user.
So email or password does not match.
I guess we while testing we changed it out.
This is the one new one and yep, this is the new one.
So let's go ahead and send this values.
Let's go ahead and send this.
It says images are required.
So that's absolutely nice.
Now let's go ahead and see that how many fields actually we have to send here, because there are a
lot of lots of up here.
Let's go ahead and move on here and looks great now.
So for this one, we are going to use the body and we are going to use the regular form data because
I need to send images as well.
So it looks okay.
Let's go ahead and start with that.
So first, let's go ahead and add a name.
This name is going to be classic.
Classic.
I write code.
The next one is price.
So let's go ahead and send a price.
Let's just call this T-shirt as a 599.
Then we got description.
So we're going to say a classic.
T shirt.
Come on.
A classic t shirt for coders.
Okay.
So next up is category.
So let's go ahead and say Cat Go Ri and we need to bring up the category from here.
So it should be something of the enum.
So let's go ahead and yeah, this is a short sleeves.
So yeah, this is all the data that needs to be filled up.
So this is short sleeves and then after the category, we need to put up the brand and I guess that's
it.
These are only the information that we got.
And we can shrink this.
There we go.
Next up is Brand.
So this one is learn code online brand and this one is photos.
So make sure this is photos and we need to change the type of this one.
So this one is going to be file.
I'll be selecting actually multiple files this time.
So I'll go into my folder where these all images are being placed.
Images t.
Coda and this one is a code D, so I'll just select all of these guys, four of them really a lot.
But let's go ahead and select.
So it says for file selected and the moment of truth how many errors we got.
Let's go ahead and send this one up here.
And I'm expecting an error here immediately.
Hopefully.
And I'll tell you the reason why I'm expecting this, because something I just quickly realized.
Okay.
This is taking a bit while.
Okay.
So we got all of these stuff and okay.
Everything is looks good now.
Why?
I was expecting the error.
Let me show you that.
Because inside the cloud area, I didn't created the products folder.
So I was expecting that this is going to give me an error, but looks like they have created a folder
for me.
It used to be an error in earlier days.
It looks like they have updated some stuff.
If the product doesn't exist, it's going to create that.
So now inside the products, I can go ahead and say let's browse this.
And yep, all of the t shirts are coming in nice and easy and I can see in the response.
Let's also analyze the response.
Are we getting all the data nice and easy so we have got success through the product is name price description
photos.
Yep, nice and easy error category short sleeves brand learn code online rating is zero.
Number of reviews is obviously zero user.
We have injected that the ID this is the product ID itself.
The reviews are empty right now and created at fantastic.
Didn't expected this to go this well but hey this is all good.
Now one more thing I would like to check is inside here.
So let's go ahead and refresh this database, refresh that and we got a product and I can see that my
product is now available.
Now let's go ahead and try out one more time because I'm obviously looking to have a little bit more
of the details.
Let's go ahead and work with that.
So this one is going to be.
Next one is going to be parole coder T-shirt.
And again, there is an extra edge pro coder.
T shirts again.
T-shirts.
And this one is since a pro order.
Let's go ahead and make it nine, nine, nine, because it will help us to understand the filtration
and everything really nice.
And let's go ahead and change the description.
A pro coder, a pro t shirt for pro coders, and the category is a short sleeves.
This one is same and the brand is learn code online and let's go ahead and select newer photos this
time.
So I'll go one step back and these are pro coder t shirt again.
I have four of these images, so let's go ahead and open them up and let's go ahead and send this one
again.
So admin is able to upload these.
And again, these upload is going to take obviously some time because the images are getting uploaded.
We are receiving the request, then we are storing that into database.
So this obviously is going to take some amount of time and there we go.
Fantastic.
Now we have all got all of that.
Now it's time to test one more route.
Let's go ahead and save this one.
And this one is going to be not for admin but actually get all products.
So let's say get all products and this can be accessed by anyone and including the admin.
This is rather a get route.
So save that and let's see in the code what we are calling the route itself, we are calling the routes.
There we go.
So slash products, that's it.
Let's move back up here.
And slash products.
Let's go ahead and simple get request logged in or not?
We haven't bothered about that.
Let's send this one.
It says cannot read property length of undefined.
So it looks like there are some issues in getting the request.
Hey there, everyone.
Hey, Tayshia.
Welcome back.
And it took me a little while to actually solve this issue, but I want to walk you through that, how
I solved it and what was the exact process of it.
Now, while solving any of the bugs bugs, you first need to stay calm and make sure that you actually
follow that.
What are the errors are giving in?
Just follow these errors.
Follow the lead.
And probably within a day, probably two or maybe five day, you will able to figure it out.
Now, this was pretty quick of a debugging, but still took some time.
The first property or the first problem that I was getting is I cannot read the property of the length.
Now what is the issue going on?
Why I'm not able to get the product stolen after a little bit while I realized that this products is
not actually a product because this is being an object created from the custom class that we have designed.
Our custom class has property of DOT base has a property of DOT query, but we don't have the property
of dot length.
So there are two options in front of me.
I go ahead either and create another method here and it actually sends me back by calculating the length
of whatever the base I'm sending in.
So whatever the final product dot basis, I need to actually calculate that or rather a better approach
would be to not call it as products, but actually a products object because obviously this is an object
from a very class that makes sense.
Now further down the road I need to create available.
So let's call this one as products.
Why variable?
Because this product is going to change quite a lot.
Maybe because of pagination, maybe because of some queries.
So this is going to be changed quite a lot.
And further down the road, I'm going to go ahead and say, hey, products object, give me a base from
you.
This can give me base and the query both.
So the base is actually products dot find.
So I go ahead and further down the road now since the product is creating created from this base now
I can actually go ahead and run this product dot length.
Now I go ahead and save this one.
This actually didn't solve my problem, actually created a whole lot of other problems.
I send this one and now it says, Hey, I cannot read the property base.
And I was like, Wow, why is it not able to access the base?
It is clearly mentioned that I do have this base and the big query, so probably there is something
wrong with whatever the values I'm returning.
So in the search I'm returning the this.
That means this constructor.
So actually I'm returning all the values in the filter.
I found out that I am not returning anything, so I go ahead and simply say return this.
So I thought this should fix majority of my issues.
So let's go ahead and test it out one more time.
I try to go ahead and send this out and it says, Hey, cannot product start.
Pager is not a function.
And I realized, okay, y products start pager is not a function because it's, it's a function accessible
by products object.
So let's go ahead and copy this.
And now whenever we are calling this pager, we obviously need to call it not by products, but actually
the products object.
Now similar to this, when we are actually creating another products here again, then it cannot be
created with products base.
It needs to be created from products object base because that object has the property of the base.
I go ahead and save this one and I was expecting okay, well this seems all done and I have returned
to the response page and I'll be able to send all the response.
Now, I went up and I send this and I saw all of these going on nicely.
So I'm getting all the properties and products and everything nicely.
But there is still one issue that is something which I guess I'm not getting it right now.
That's because I'm not holding the products here because this needs to be a weight.
And hopefully, yeah, this is exactly what I did because I realized that products dot base might be
changed a little bit, might be getting some values from the database.
So let's await this one.
And when I awaited this probably this is yeah, this is exactly the error that I got into.
So if I don't have it, that gives me the all the values that I wanted to.
But if I wait for it, it says query was already executed and I have not seen this error in a while
because I don't use this exact flow into the applications here.
So what was the drawing going on wrong?
Again, StackOverflow helped me in figuring out that what is going on.
So here is here is something that we got an answer up here.
So again, this was asked just two months ago.
So this is a recent update that has been added.
And thanks to StackOverflow that we got this one, so they says, hey, I had the same issue and I changed
a dot clone and this is going to solve the problem.
And but not only here, I still went up and read that what actually is the problem.
So Mongoose Dot Connect always returns a promise, not a mongoose instance and all of that, but it
says duplicate query execution.
So if you have some of the change query going on like dot, find a dot something on fine and on top
of that.
And so it doesn't allow all of that.
All you got to do is chain a dot clone as they say up here that I changed it up.
So what I did in that that hey whenever you.
Need to actually run this base and you are firing up more of that.
Just go ahead and add a dot clone here.
So I go ahead and save this one and I go ahead and run this one and there we go.
It now gives me all the product.
Now, to my surprise, I was a little bit surprised that why not putting the away.
It actually gives me the result which I want, but the proper and the right way of doing the things
is actually having a dot clone being chained up whenever you are firing up two or more methods.
And I can definitely add this into a couple of more methods.
Now, the reason why I'm showing you the entire process instead of just magically doing video editing
and all of that is because it is important for you to understand that what is the process of debugging?
And nobody is immune to debugging process.
Everybody makes their mistake and the code eventually is going to be refined and refined as we move
on further down the road.
But I hope this actually gives you a brief idea that how we actually need to work on with the things
and how things are going on.
So this is now a better method as get all products and as well as this admin route that we have created
for ADD all the product.
So there we go.
Finally, our debugging is all done and now we are able to access all the product.
And I hope you enjoyed the part where I walked into through the every single point and what was going
in my brain while solving this debugging process.
Let's go ahead and catch up in the next video.