Hey, did everyone that they share?
And in this video, we're going to have a revision of the JavaScript.
Now, in case you think that you are an absolute pro in JavaScript, you don't need this video, then
you can absolutely skip this one.
This has nothing to do with the course just except one thing.
Let me just walk you through with that one thing and then you'll be absolutely free.
So in the product.
JS What we have done is we have used these dashes inside the enum.
When we are setting up the categories, I later on realize that these dashes can actually give us some
problems.
So I'm going to go ahead and remove all these dashes.
Now, usually you can go ahead in the front end, can use the dashes or even spaces, whatever you like.
But I think when these values are going to come to us, they might create some issues, not compulsory,
but they might.
It was bothering me a little bit because sometimes these dashes and spaces are too much are encoded.
I don't want that.
So that's why.
Okay, so this is the main part of the thing that is about or related with the course.
Now next step is the JavaScript basics.
I have realized that a lot of people don't really understand some of the concept of the JavaScript and
that actually bothers them throughout the course.
In this video, I'll walk you through with one of the such incident, which is throughout the industry
is being used, which is one to replace method.
I will walk you through what happens and how it can be utilized and further more on that.
Okay.
So we're going to first start by having a basic file.
So first let's get a simple file inside the util folder.
It's not going to be useful.
It's going to be a simple new file, which is going to be simply a demo test.
JS Okay, so this is the basic file that we have, and I'm going to be just writing some gibberish code
in this.
These codes are not going to actually run, but this is important for understanding perspective.
Now what we have so far seen that whenever we have modeled something like user or product or whatever,
we have this method of find.
Now in the find so far we have passed on nothing.
That means an empty parenthesis, or sometimes we don't even pass on that when we don't pass on anything.
This means that we are passing in an empty selector here.
Now this is where usually the query goes in of the MongoDB.
In case you remember, we sometimes actually worked on something like this that, hey, when the email
equals to email or something like this, or in this case we were writing just the email.
Now the idea behind that is that if I want to find a user which is having an email of, let's just say
hit at the rate AE dev, it's going to give me all of that.
Now in case if you remember, we used similar kind of property when we were working on the models.
Let me show you that again.
Inside the user controller for the manager especially, we wrote that I want to find everybody who's
role is user.
So I can definitely go ahead and find the user based on this query.
Now so far we have seen that I can find all the users who are having role some particular email, very
restrictive on that or everybody like that.
But there is a little bit more to it.
Let me show you that on the documentation.
If you will read the MongoDB manual itself, you are going to see that we have some of the comparison
query operator as well.
These query operator are remarkably helpful in all of the cases they have equals greater than greater
than equal to in then less than less than equal to then we have got nine not in and this one is.
This one is.
What was that?
I forgot.
Okay, not equal.
So there's a lot of things available up here majority of the time that we use.
Is this not in that if you want to check whether something some of the values is inside that area or
not.
But in this case, we are going to use only the phone here.
In fact, some of the time I have seen majorly to being used greater than end the less than these are
two, but we are going to be using greater than equal to also.
So what happens in this case you can see in the greater then notice here they are saying DB don't inventory
don't bother too much on that.
Just remember we have this find method and inside this fine method we have the same parenthesis and
inside that we have these curly braces.
Inside this curly braces we are passing on a quantity.
And then this quantity also goes into the curly braces.
And then we go ahead and say, hey, dollar greater than all these operators actually need to pass on
as dollar.
So this dollar is really, really important and we go ahead and say, hey, this should be greater than
20, so you need to search for something whose quantity is greater than 20.
So this is really, really helpful and all the other things actually works almost exactly the same.
You don't need to probably study them too much.
You just need to just glance over them.
So this one is going to be exactly same, but it's also going to match the equality as well.
So if the quantity is 20, it's going to return that then we have less than and you guessed it right,
it's it works exactly same and we have less than equal to as well.
They work exactly same as you might be expecting them returning the value which is less than or equal
to the 20 in this case.
So this is all clear.
Okay.
Now moving on, coming back onto the code part.
So we're going to go ahead and comment this out that we understand this much better part.
Now, coming back on to the user controller.
So this is the basic user controller that we have.
We don't want to work on the user.
We want to work.
The model controller.
So if I can find not model product controller, my bad.
All I want to do here is I want to show that how it looks like.
So I'm going to go ahead and simply say, hey, log me all the requests to dot query.
So anything that's coming up into the query format, I just want to go ahead and see how it looks like.
And this needs to go above.
There you go.
Now it's all happy.
So anything that is going to come up into the question mark format is going to come in.
So how this query actually looks like we are going to be reaching to the slash API, obviously slash
V1, whatever is your path in this case.
And then maybe you're searching for user or maybe you're searching for product.
Now most of the application, when you are searching for maybe a shopping website, maybe a travel booking
website, go AB or whatever you are searching for a hotel product, whatever you are searching for.
The first thing is actually search and anything after the question mark is actually a query.
So this is the by default.
So what happens is inside the search, you pass on a kind of a a word that you are looking for, searching
probably a simple string or something.
So in our case, let's just say something.
Someone is searching for a code related t shirt.
So I'm going to say code or code or whatever you like to say this one.
So now this can be a long string and change with the plus keyword and however you want to separate them.
But in this case, we are going to be just choosing one word, just for an example.
Now, not only that, there is one more keyword up here, so we use to separate these keywords and in
person sign, this is how the query is being crafted.
Now here you're going to see there is a keyword again, there is a page.
So page can be equal to one or maybe two.
So whatever you are doing, one or two, something like this.
So sometimes you restrict the products that I don't want to show entirety of 200 or 2000 products on
my website, I want to separate them on the page.
So pagination goes in there.
So you want to get a product on page number two, page number one or something like that.
So this is also comes in as a query part.
Now, not only that, sometimes you have, let's just say a category as well.
So you go ahead and filter it out based on the category.
So I'm going to go ahead and say, hey, category is equals to and then we go ahead and say this one
is short sleeves.
So this is the category as well.
Further down the road, a user also wants to see that.
I want to have only the t shirts whose rating is above four or maybe above three.
So in that case, what we can do is we can go ahead and say, hey, rating and equals to four.
Now how you actually craft this URL or this query is up to you.
Now, usually a lot of people prefer to just get it as like rating equals to four and do all the manipulation
on per root method.
But you can actually go ahead and say something like this inside this bracket.
You can say rating greater than or equal to four.
So this is up to you what you pass on inside the rating.
So at the time of in the front end itself, how you are crafting is heavily going to be depends.
So this information needs to be 100% passed on to somebody who is playing in the front end.
So again, in this case, let's just say we are going to go ahead and say that we accept parameters
in this format.
So user will be selecting just the four and three.
But the way how this is going to be crafted in the front end is going to be like this.
So inside the squared brackets, we just go ahead and grab get.
There is a simple and very important role of why we are crafting that into a square bracket.
Now, further down the road, this is not enough for me.
I would like to have a little bit more of the query.
So I'm going to go ahead and say, put another person and I'm going to say I want to restrict the pricing
as well.
So I'm going to go ahead and say, hey, pricing should be, let's just say, less than or equal to.
I am looking for less than equal to maybe nine, nine, nine and I need to move that on to the just
like that.
And I can actually just for making sure you understand the things.
I'll move this one here.
And pricing is less than or equal to nine, nine, nine.
And I also want to select the starter range as well.
And price price is also going to be greater than or equal to 199.
So you are looking for a range.
So yes, this kind of complex query is very absolutely normal thing to deal with that this is the reason
why we are actually working on the product in the email we have done with a different kind of complexity
about the tokens and all these stuffs, but this is also another kind of complexity that works on and
goes through with that.
Now, as complex as your product is going to be, these queries may become a little bit longer as well.
This is a very common phenomenon that happens quite a lot.
Okay.
So how we why this is important for us to discuss this part?
It is very important.
First, I want to walk you through with a little bit onto the other thing.
So I'll take you on to a website which is an Mozilla developer network here, Mozilla developer, kind
of a blog.
Okay.
So this is the page where the demo is the string replace.
We have already seen that.
How it works on it gives you to example there is a quick brown fox a.
Example here it says console.log in this entire paragraph, just replace the dog with a monkey.
We got that.
We'll get the result here.
And also, it gives you an example of regex or regular expression in which you replace the dog with
the ferret.
Let's see how it works on.
So we see the two output here.
The quick brown fox jumps over the lazy monkey.
So notice here this time it says p dot replace.
So the first time it says it sees the dog, it replaces it with the monkey.
Acceptable.
Totally doable.
In the second case, the p dot replace.
It says wherever you see the dog first time, just replace it with the ferret.
So both of them are giving you the exact same result.
We're not going to be using it this long example.
We'll be actually crafting this example, something that we actually like to know and we like to work
on.
So we'll be having a GTI, GTI and LTY, so greater than equal to greater than equal to end LTE.
But we just saw that they alone doesn't work like GTI and XLT and in the URL itself you cannot pass
$1.
It will automatically get converted into URL and coded just like space is converted into person 20.
So the problem is the query that I'll be receiving up here.
Notice here what we are sending here is having a GTI.
Now I need to first worry about that.
How can I make it as a dollar GTI?
Because that is the query I can go ahead and pass on here in the email segment.
Remember, let me just copy this line up here.
So if I need to pass on something like this, this is important for me to convert that.
So let me go back up here and inside this example.
We are going to go ahead and paste this one.
So now if I want to pass on this quantity, I have to pass on as dollar to 20.
So the challenge one itself is to how to replace this price with something like price dollar GTI because
dollar is not coming to me and shouldn't be.
This is also a security risk, so let's go ahead and work out with that.
So I'll go into this one and we have this GTI now.
We have seen something like this replace already in the past.
We have seen that we were looking for something like better and then this pace and we were saying that
hey, whatever comes in, just go ahead and use like this.
So what it does actually, it gives me just the empty token.
We have already seen that.
Very understandable.
Nothing big deal.
We are going to go ahead and remove this entirely.
We're going to focus rather on the regex itself this time that how we can use the regex and do some
stuff.
So instead of the dog itself, I'm going to go ahead and say I want to look for GTI.
Remember, two slashes are really important for these regex and then slash, I will see that what it
actually works or not and how it actually works with that.
Let's run this one.
So it replaces whatever it finds it with the ferret.
I don't want a ferret like this.
I would rather like to convert that into a dollar GTI.
The ultimate goal is not to just put a dollar on GTI, but actually on all of them GTI.
Then again GTI.
Then I want to put a dollar in front of all of them.
So if I go ahead and do this right now, it, it places the dollar at one of the keyword, but not all
of them.
So I'm somewhere there, but it's not doing the job, all the places.
Now, what if I just go ahead and add all of them?
Let's just say I add LTE, also LTE.
So obviously this is not a correct string.
So what if I do?
I put up a or statement in between them.
Let's go ahead and run this.
Okay.
Now it's still it says, hey, I'm putting it up on the GTI, not on the LTE.
So clearly this is not working.
But let me break it to you already that no matter what you're going to do, you're going to use the
OR character.
So I'm going to go ahead and say, hey, GTI or you find LTE or whatever is the matching keyword you
are looking for.
But the problem with the regex is they are too quick to actually give you the output result.
They actually go ahead and find whatever the first character is and they replace it with this guy.
But there is another problem here.
We have all of them, but this is not giving this is where the another magic of this comes in.
Now, what you can do, this replace is a really complex one.
So whatever the regex you are matching for, you can actually kind of throw a callback here.
So whatever it matches, it actually gives you as a kind of a function back and you can just fire up
a callback and into this whatever you can, you can actually return that or whatever.
For example, if I just go ahead and say test, I go ahead and run that it says, Hey, test is not
defined.
So we actually can replace that with some kind of a string here.
Let's go ahead and do that with it.
Test a string and run that again and notice that the there it says test.
Now, I don't want to replace it with the test.
Obviously I want to replace it with the dollar GTI.
Okay, let's go ahead and do that one by $1 GTI.
Run that.
Okay, this one is working fine.
Now only thing I need to worry about is I need to figure out that how can I place dollar in front of
all of them?
How can I match all of these guys?
Okay, so the first thing to do in all of that is I have to replace this I with G.
G stands for Global.
If I run this one now, it says, okay, you have a dollar GTI you.
Every dollar and you have a dollar.
So what happened this time?
Whatever the first match is, it just replaces all of them.
With these guys, we can actually go ahead and change this one.
The reason why I'm writing em here, because this is a variable here.
I can use a variable.
So I'm going to go ahead and say, hey, remove this one and use a back tactics and the back text.
I can say simply dollar and this, this is how we declare variable in them and we are going to go ahead
and save match and I'm going to go ahead and say a match and after that I'm going to go ahead and write
test just to show you.
I notice here it says g t test, g t test and l t test.
So whatever all it is matching in the global now this is being used.
But this test is actually just to show you this is not how we are going to use that.
What we want to do after the dollar is variable, but we want to add an additional dollar just like
we added a test.
After that, we're going to go ahead and use a dollar before it.
I'm going to go ahead and run this.
Okay, so now it looks like my problem is solved so I can have a dollar g t, dollar g, t t.
So whatever the cases I want to batch, I can go ahead and use that.
But there is a small problem.
What if in the future something comes up as my g t.
So if that comes in notice here it says my dollar.
Okay, so this is just a kind of an odd case that we are facing up here.
And in case you have worked any time in the past with these rejects, you know that these are really,
really tricky one to handle.
So in this case, what you can do is first, you can wrap whatever you want to match up here just like
this.
So wrap them in the parenthesis and then you can go ahead and simply define a boundary for the boundary.
The slash B is being used.
Now, if I run this one, it only matches either the values on the start or at the very end of it.
And not not like end end of it.
Like the string should start with only anything after that or previous that is not going to be converted.
So we are strictly matching for just the values that you have given to me.
Now it works in majority of the cases, but in the case of JavaScript, I have noticed that there is
a slash B at the end of it is also required in some cases actually fails out.
So the slashing are a trailing slash B again, this is same for boundaries.
It is being required.
So now whatever you pass on into this string, this is going to be converted as the matching string.
So this is going to be very helpful for us in the future cases.
Okay.
So we have done a lot of detailed discussion about how regex works and how we can replace the values
and how this actually gives you a method and all of that.
In fact, if you're going to read all of this, this is all written up here that hey, actually this
replace is a function replacement and all of that.
So yeah, this is all written up here.
It's not something I'm coming up magically out of the air.
Okay, so this is all done coming back onto the code part.
Now one problem is solved that I can go ahead and into this string.
I can go ahead and inject dollar at every single piece of this.
But in reality, this is really important to see up here.
When I say quantity, for example, I'll say, okay, price or not price.
Actually, in this case, yeah, we can actually use price.
So I want to say price and price should be greater than 199.
So how I'm going to craft this query in order to first craft that how this query actually works, I
need to see that how it looks like and that's why I have added console.log request to query.
So I'm going to go ahead and copy everything from slash product to everything.
So I'm going to go ahead and save this one and I'm going to put a comment on this one as well.
And let's go on up here.
And when I was testing this test product, I actually added up a lot of code and stuff.
So this is exactly what I tried before.
Actually, I showed it to you.
So this is what I was trying.
Okay, moving on.
So if I go ahead and paste this one again, I need to remove another slash.
So this is the same query product question mark and all of that.
And notice here in the params, they are also saying all these things that you are looking for.
So gTLD ratings category and again remember this G20 always put on the fields which are actually numbers.
Let's go ahead and send this and see what happens.
I go ahead and send this and this says, hey, this is.
Test product.
Why cannot I get it?
Okay.
It looks like it is.
Yeah.
Are we not getting it at all?
Request dot query it as product should be there.
Let's go ahead and try it one more time and send it.
And says, Hi, I cannot get it.
Let's see that this is a slash test product slash product.
Okay.
I don't need to send the product here.
Actually, I can just go ahead and say.
Question mark.
So this was wrong.
Let's send it again.
And there we go.
Finally, we have received this.
Now, the important thing which I want you want your attention on this one is this part.
Notice here it actually crafts the entire query into this syntax.
So notice here it says search.
And the search is coming up as a standalone object in itself.
The page is coming up as a standalone object.
The important part is this rating.
Now, notice here the slash end is also coming up because I actually copy pasted the stuff from the
browser.
Usually it doesn't happen, but notice here I can actually go ahead and copy this and go back into the
code part.
Into this one.
And for a moment, let's just uncomment this one and try to compare what we have received.
So we have received something like this, which is rating and we actually don't have the slash.
And also this was just from copying and pasting.
Now can you see how much similarity is there that if I want to use this a user find, I can actually
go ahead and inside this parenthesis, I can just create a bracket up here and can pass on this query,
which would be instead of looking the quantity, it would be saying rating.
There we go, nice and easy.
And after that, we already got this parenthesis.
And inside the parenthesis we are getting a GTI and that is equal to four.
Now obviously this four is right now a string because it is coming from the URL and all these stuffs,
but it will be automatically be converted or we can actually convert it into a purely number format.
Importantly here is that I have now the knowledge to convert this GTI into the dollar GTI and that's
all it takes to find the query up here.
And we will be using this exact same logic and syntax that I walked you through in this video.
So it is really important for you to understand that what we are trying to do and how we are actually
doing it.
So we are going to go ahead and take all the query and when we take all the query itself, we are going
to work like that.
Now again, there is a little bit more to it, a little bit more to it, not just only this.
Now notice here this entirety of the thing is coming up as an object.
Now, our replacement method actually works only on string.
That is a string specific matter.
And in order to further put this data into the find query itself, then we have to convert it back into
an object.
So please remember this always that whatever the data comes up is going to be string.
String first needs to be converted into JSON.
So JSON we can use the methods of parse and string, ify and all of that and we can work through that.
I know this is a really long video, but this kind of video is really important because that's whole
what we are focusing on to the entirety of the operation.
So remember the steps we have already studied that entirety of the pagination search filter.
This is all what we are taking in just one single string and this is the real use case.
This is how you will be actually coding this stuff.
So I hope you have understood this one.
I know really long.
So a little bit boring as well, but this is really important topic.
In the next video, we are going to go ahead and continue to write our product controller.
I'm going to go ahead and comment this part as well so that it remains with you as an exercise.
And not only that, I will also give you a little bit of this URL as well, so that later on you can
practice this stuff.
So let me go ahead and add this one.
So this one is actually your URL, just like that.
And let's go ahead and comment this.
And also let me give you further down the road.
This example also, maybe you want to have some more code with that.
So this is all again, this is all a test file.
I won't be like giving you or something more onto this one.
This is it.
Okay.
Yes, that's all.
All about it.
Let's go ahead and move on to the next video and finally continue our journey into the product model.
Let's go ahead and catch up in the next one.