In this video we're going back to camp to make a quick refactor.
So as I mentioned in the last video about module that exports we're going to take some of the model
code this campground schema the campground model and we're going to push this out into a separate models
directory in a campground file and that will pave the way for us to do it with our other models like
user and comment.
So we'll get there in time.
But for now we have three main steps we need to create the models directory and the campground file
.
Then we need to use module but exports inside that campground file and then we need to require everything
correctly including requiring mongoose and requiring the newly created campground file.
So before I get started I do have a v3 that I'm working in now because we're making some larger changes
.
We're moving files around.
So if you want to follow along the new changes will be in version 3.
Ok let's begin.
The first thing I want to do is make a directory called models and instead of models we'll have all
the model files.
So I'm going to make my first one now.
Touch models slash campground.
Yes just like that.
And then I'm going to open that file up
and I'm going to put all of the campground logic which is just this right now and it doesn't seem like
it's a huge benefit.
It's what five six seven lines that we're cleaning up.
But when we have multiple models which is coming soon it will be 30 or 40 different lines of models
and we don't want our apt to be cluttered.
And we're actually going to even clean things up further in a future video.
We're going to move all of these routes out into a separate file multiple separate files to make the
app really clean and as minimal as possible.
Let's go to a camp ground J.S. and paste this code in and we're getting an error right away.
It's telling us Mongoose is not defined.
That's one thing we need to take care of.
And that's nice and easy for mongoose equals require mongoose just like that save.
And we have our schema and then we're just going to do a module that exports equals mongoose top model
campground campground schema.
So when we require this file on read you require campground J.S..
We'll be getting the model.
So now we need to go to actually yes and just add a nice required statement up here and we can just
do that right here campground equals require and we have to do dot slash models slash campground.
We don't need the data.
Yes that's implicit and we don't have to name it campground but I'm going to keep it campground because
that's what we're using down here.
We're doing campground find and campgrounds create.
All right.
So we can test this out and see if it works.
All we have to do is start the server and if we see our campground today having the database that means
that our model is set up correctly.
Otherwise you don't have a way of getting the campgrounds out of the database.
All right.
So let's try that now.
Let's run our app.
J.S. looks good so far.
Let's refresh the campgrounds page and here are the four campgrounds that I added.
These are some new ones that you haven't seen yet.
I just went in and added some new campgrounds.
So we have this beautiful glacier camp Lakey Laiki or lake leaky.
All right let's go back now and we're done with everything we need to do the next iteration of this
will look like comment equals require and we'll do the same exact thing dot slash models slash comment
and we'll do the exact same thing again for user when we have user log in.
So now we have this nice pattern where we don't have to have all the code cluttering up G-S we can require
different files in.
And the key to that is module exports.
If I leave this off just show you that now if I have the right code in here but I don't export anything
and don't add it to module but exports when I require it.
This line right here require model slash campground.
I'm actually going to get an empty object like that and I'll get an error because when I try and do
something like empty object I find that I'm not going to work for us and I'll show you that restart
the server started up again.
Refresh the page object object has no method find.
As you can see here.
Object object has no method fine.
That's because we're not sending this mongoose model out of the file.
We're not exporting it.
Now we are refresh again restart server and things go back to normal.
So that one line is really important.
It's not enough just to put things into files but we have to make sure that we're using mossel But experts
correctly