1
00:00:00,380 --> 00:00:04,250
In this video we're going back to camp to make a quick refactor.

2
00:00:04,290 --> 00:00:09,090
So as I mentioned in the last video about module that exports we're going to take some of the model

3
00:00:09,090 --> 00:00:14,850
code this campground schema the campground model and we're going to push this out into a separate models

4
00:00:14,850 --> 00:00:19,950
directory in a campground file and that will pave the way for us to do it with our other models like

5
00:00:20,040 --> 00:00:21,460
user and comment.

6
00:00:21,630 --> 00:00:22,640
So we'll get there in time.

7
00:00:22,710 --> 00:00:27,720
But for now we have three main steps we need to create the models directory and the campground file

8
00:00:27,720 --> 00:00:28,380
.

9
00:00:28,590 --> 00:00:33,540
Then we need to use module but exports inside that campground file and then we need to require everything

10
00:00:33,540 --> 00:00:38,710
correctly including requiring mongoose and requiring the newly created campground file.

11
00:00:39,150 --> 00:00:44,730
So before I get started I do have a v3 that I'm working in now because we're making some larger changes

12
00:00:44,730 --> 00:00:44,900
.

13
00:00:45,000 --> 00:00:46,320
We're moving files around.

14
00:00:46,470 --> 00:00:50,020
So if you want to follow along the new changes will be in version 3.

15
00:00:50,460 --> 00:00:52,230
Ok let's begin.

16
00:00:52,230 --> 00:00:58,710
The first thing I want to do is make a directory called models and instead of models we'll have all

17
00:00:58,710 --> 00:00:59,700
the model files.

18
00:00:59,910 --> 00:01:01,680
So I'm going to make my first one now.

19
00:01:01,830 --> 00:01:04,710
Touch models slash campground.

20
00:01:05,010 --> 00:01:07,120
Yes just like that.

21
00:01:07,500 --> 00:01:09,000
And then I'm going to open that file up

22
00:01:12,690 --> 00:01:18,060
and I'm going to put all of the campground logic which is just this right now and it doesn't seem like

23
00:01:18,470 --> 00:01:19,930
it's a huge benefit.

24
00:01:19,930 --> 00:01:22,650
It's what five six seven lines that we're cleaning up.

25
00:01:22,830 --> 00:01:28,260
But when we have multiple models which is coming soon it will be 30 or 40 different lines of models

26
00:01:28,290 --> 00:01:30,520
and we don't want our apt to be cluttered.

27
00:01:30,630 --> 00:01:34,320
And we're actually going to even clean things up further in a future video.

28
00:01:34,320 --> 00:01:39,000
We're going to move all of these routes out into a separate file multiple separate files to make the

29
00:01:39,020 --> 00:01:42,060
app really clean and as minimal as possible.

30
00:01:42,540 --> 00:01:47,760
Let's go to a camp ground J.S. and paste this code in and we're getting an error right away.

31
00:01:47,760 --> 00:01:50,330
It's telling us Mongoose is not defined.

32
00:01:50,370 --> 00:01:51,960
That's one thing we need to take care of.

33
00:01:52,140 --> 00:02:00,860
And that's nice and easy for mongoose equals require mongoose just like that save.

34
00:02:01,290 --> 00:02:08,310
And we have our schema and then we're just going to do a module that exports equals mongoose top model

35
00:02:08,840 --> 00:02:10,610
campground campground schema.

36
00:02:10,890 --> 00:02:15,050
So when we require this file on read you require campground J.S..

37
00:02:15,300 --> 00:02:17,490
We'll be getting the model.

38
00:02:17,550 --> 00:02:22,650
So now we need to go to actually yes and just add a nice required statement up here and we can just

39
00:02:22,650 --> 00:02:34,200
do that right here campground equals require and we have to do dot slash models slash campground.

40
00:02:34,200 --> 00:02:35,080
We don't need the data.

41
00:02:35,130 --> 00:02:41,100
Yes that's implicit and we don't have to name it campground but I'm going to keep it campground because

42
00:02:41,100 --> 00:02:42,980
that's what we're using down here.

43
00:02:43,010 --> 00:02:47,660
We're doing campground find and campgrounds create.

44
00:02:47,730 --> 00:02:48,800
All right.

45
00:02:49,230 --> 00:02:51,560
So we can test this out and see if it works.

46
00:02:51,570 --> 00:02:56,910
All we have to do is start the server and if we see our campground today having the database that means

47
00:02:56,910 --> 00:02:58,440
that our model is set up correctly.

48
00:02:58,560 --> 00:03:02,130
Otherwise you don't have a way of getting the campgrounds out of the database.

49
00:03:02,130 --> 00:03:02,750
All right.

50
00:03:02,850 --> 00:03:04,090
So let's try that now.

51
00:03:04,560 --> 00:03:06,260
Let's run our app.

52
00:03:06,380 --> 00:03:09,920
J.S. looks good so far.

53
00:03:10,230 --> 00:03:15,270
Let's refresh the campgrounds page and here are the four campgrounds that I added.

54
00:03:15,270 --> 00:03:17,590
These are some new ones that you haven't seen yet.

55
00:03:17,640 --> 00:03:20,130
I just went in and added some new campgrounds.

56
00:03:20,130 --> 00:03:25,540
So we have this beautiful glacier camp Lakey Laiki or lake leaky.

57
00:03:25,980 --> 00:03:31,740
All right let's go back now and we're done with everything we need to do the next iteration of this

58
00:03:31,930 --> 00:03:41,210
will look like comment equals require and we'll do the same exact thing dot slash models slash comment

59
00:03:41,700 --> 00:03:46,940
and we'll do the exact same thing again for user when we have user log in.

60
00:03:47,640 --> 00:03:53,070
So now we have this nice pattern where we don't have to have all the code cluttering up G-S we can require

61
00:03:53,070 --> 00:03:54,270
different files in.

62
00:03:54,390 --> 00:03:56,960
And the key to that is module exports.

63
00:03:57,150 --> 00:04:02,820
If I leave this off just show you that now if I have the right code in here but I don't export anything

64
00:04:02,820 --> 00:04:06,420
and don't add it to module but exports when I require it.

65
00:04:06,750 --> 00:04:09,540
This line right here require model slash campground.

66
00:04:09,690 --> 00:04:14,760
I'm actually going to get an empty object like that and I'll get an error because when I try and do

67
00:04:14,760 --> 00:04:21,740
something like empty object I find that I'm not going to work for us and I'll show you that restart

68
00:04:21,740 --> 00:04:24,180
the server started up again.

69
00:04:24,180 --> 00:04:28,600
Refresh the page object object has no method find.

70
00:04:29,160 --> 00:04:30,580
As you can see here.

71
00:04:31,380 --> 00:04:33,440
Object object has no method fine.

72
00:04:33,600 --> 00:04:37,350
That's because we're not sending this mongoose model out of the file.

73
00:04:37,350 --> 00:04:38,520
We're not exporting it.

74
00:04:38,610 --> 00:04:44,510
Now we are refresh again restart server and things go back to normal.

75
00:04:44,640 --> 00:04:46,500
So that one line is really important.

76
00:04:46,650 --> 00:04:51,180
It's not enough just to put things into files but we have to make sure that we're using mossel But experts

77
00:04:51,180 --> 00:04:51,630
correctly
