Welcome back.
So this video is going to be pretty quick.
I'm going to show you something called modules on exports which is going to help us clean up our code
and make it more modular.
And then I'll show you how we can use module but exports to clean up the code that we just wrote which
is the reference is genius.
File this one here.
Currently it's a long file that has our mongoose code up top.
Then we have the post schema and model and then the user schema and model and then all of our subsequent
code where we're using those models what we're going to do is break this out into separate files.
So the post code here all the stuff to make the schema in the model will be its own file that we can
require just like we are requiring mongoose here.
We can do the same thing.
It won't work just yet.
But we can do something like var post equals require.
And then we'll have post-up.
Yes that's not exactly the syntax but it will be a require statement and we'll do the same thing for
the user.
So why would we do this.
There's two answers.
First one is that it will help us clean up our code and shorten this.
So we'll get rid of all of this and move it to separate files.
But also it helps us make our code more modular.
And if I wanted to have another application that use the same user model or another file that used it
I wouldn't have to duplicate the code.
I could just require that code in that file as well.
So two main goals we will be using when I'm about to show you to also modularize are actually yes instead
of the Yelp camp application this is getting pretty long.
You have a bunch of routes.
We have all the model code up here and this will continue to grow as we get more and more models.
So after this video I'm going to show you how we can clean this up.
But to start we're going to work with this references dodgiest file.
And the first thing I'm going to do is move all of this code that relates to creating the model.
The post model and I'll move it to a new file and I'm going to make a new directory first called models
and the file will be models.
Slash post.
Jay Yes.
And then while I'm here I'll also do models slash user dodginess just like that.
OK.
And then I'll open both of those up.
Let's start with post models slash post.
Yes.
OK.
And we can paste that code in.
And the first thing you notice is that we now have an error.
It's telling us that Mongoose is not defined.
So we can start by defining mongoose and this is something that you just have to do if you're modularize
in your code if you're splitting it up into small pieces.
You're going to have to require things a lot more often.
So we require mongoose and there's one major change you need to make which is that we need to use modules
led experts down here which I like to think of as a return value for a file.
So just like with the return value in a function nothing is returned unless we explicitly tell javascript
what we want to return out of the function.
Same thing with the file if we just include the file but we don't export anything out of it.
We'll actually be including Nothing will be requiring just an empty file.
What we need to do is export something and what we want to export is the model.
So we're going to copy that and say modular but exports equals mongoose up model post.
So we're declaring the schema and then we're declaring the model.
And that's what we're sending out.
We could also have done this and then said module about exports equals posts.
Either one would work.
But I'm going to do it just in one swoop here.
So we'll get rid of this and add this back down here.
OK so now let's see how we've required this file inside references.
Yes I'm going to comment out pretty much everything else for now.
Well keep the user schema there but I'm going to get rid of all this just so that we're not running
anything.
All I want to do is make sure that we can import this code.
So it's going to be var post require.
And the file is post-up.
Yes we can just refer to it as post but it's inside the model directory.
So models slash post and save and try running this file and see what happens.
So node references dot J us and we get a small error and it tells us cannot find module models.
Slash post.
And that's because we're missing one key thing when we're referencing file paths and directories in
node to reference the current directory We need a dot slash and that will reference where we currently
are which is associations.
And then we want to access the models directory and then the post file and site of that.
Now let's clear the terminal.
Try that again.
This time it looks like it worked.
We don't see anything which is a good sign.
We shouldn't see anything with that code that we have here.
OK.
So let's close out of this and now will do the exact same thing for the user code.
So we're going to take all of this cut it out and then open up the user J S file.
So that's going to be C9 models slash user data.
Yes.
And not put this code in here.
Once again we need to import mongoose.
So our mongoose equals require mongoose and save that and then we're going to do a module that exports
equals the mongoose top model that we're creating from user.
So this will send out the model that when we require this file this is the one thing that we're returning
out that we're exporting out to use.
And this doesn't always have to be a single thing like this.
We can return multiple pieces.
We can make an object here and add a bunch of different bits of data.
But this is an object remember mongoose top model generates an object for us.
So that's what we're turning will save.
Now go back to references and we're going to run var user he calls require and this should be dot slash
models slash user and let's save and make sure both of those are requiring correctly.
There we go.
And now let's try using it.
So what we're going to do is just uncomment some of this code will do this right here which is going
to make us another post with how to cook the best burger and we'll do part four.
And some jibberish.
Let's change that up a little bit and then it's going to find that user with email Babuji com and everything
else should be the same it's going to push in that post and canceled out logs and data.
So if everything worked out just fine.
This should work and we should see a user with some posts printed out.
So let's give it a shot.
There we go.
We have our user Bob Belcher Bob at gmail dot com and then we have a bunch of post IDs inside the post
Saray great.
So let's recap everything Monserrat exports allows us to break things into files.
And the reason we'd want to do that is to clean up our code first and foremost.
This makes it a lot cleaner appear but it also makes things modular and makes it reusable so I can have
another file where I require the post or the user model and I don't have to duplicate any code.
Aside from the single line in the next video I'm going to show you how we can clean up camp using the
exact same concepts.