Well come back in the next few videos we're going to work on adding comments to your camp.
So currently all that we have is a list of campgrounds.
And when I click on a campground to go to the show page we see some information about it.
But what we want to happen is for there to be a list of comments or reviews here and then a button that
says I had a new review and we can then click on that and go to a form submit a new comment.
Now take us back here and we'll see a list of comments.
So there's a lot of stuff involved in getting that to work.
We have to create the comment model.
We have to associate it with the campground model.
We have to require all the files correctly then we have to create all the routes that we have to create
all the views.
But we're actually going to start somewhere else.
We're going to start by creating what's called a seed's file.
And the point of a seed's file is that we can run it to seed our database with some data.
So right now we have four campgrounds in the database.
But what I want to do is write a file that I can run and what that file will do is empty everything
in my database to start and then it's going to go in add in three or four campgrounds.
And each one will have a few comments.
So we have some sample data to work with.
And the reason I'm doing this is so that when we add comments in we'll have a way to see immediately
if the comments are working or not.
Otherwise once we create the comment model then we'd have to get the comment new and create route working
.
And we have to do a lot more work before we can even tell if our comments are set up correctly.
So we're going to create a seed's file going to do that right now in the same directory.
I'm still working in version 3.
I'm going to create a new file called seeds that yes.
And I'll open that up.
So to reiterate in here we're going to create a bunch of campgrounds and a few comments for each campground
.
And at first we're going to get errors and this is what some people call error driven development where
we write some code that we want to work.
We get an error and then we write some code to make that error go away and then we keep doing that until
it works.
So we're going to require mongoose first
just like that and then we're going to require a campground and that will be require.
And this is in the models directory slash camp ground.
And I need quotes can't forget that.
There we go.
And then what we want to do to start is just wipe everything out of our database.
And the easiest way to do that with Mongoose is campground remove.
And we're just going to ask it to remove everything and then we'll add in a callback function error
.
And then here just add a console that log removed campgrounds
and let's see what our error is here expected error to be handled.
OK so technically that's not a deal breaker but if we do get an error we'll add cancel that log error
right here.
The next thing I want to talk about is how we're going to run this file.
So in my app taught us what I want to be able to do is actually require the seeds file and we'll call
it something like seed D.B and that should be equal to requiring dot slash seed's which is the name
of our file right here seeds and that in the same directory is actually yes.
So we need the dot slash and then I want to be able to execute it afterwards and do something like seed
the database seeded be at the beginning.
Every time we start the server over this code should run.
Right now though when we require this file we're not exporting anything.
And we want to export a function.
So let's go back and let's just write a function here.
Function seed DBI.
And we're going to move this code into here just like that and indent this a bit and then we're going
to do a module that exports equals seed DB.
Just like that.
And so this will send this function out and it will be stored inside of CDB here and then we can execute
it and it will run whatever's in this function and all that it does right now is removes all campgrounds
.
So if it works when we load up the page all the campground should be gone.
Which seems a little counterintuitive for now but this will make more sense when we have the Creation
working too.
So let's do this now.
Node Aptor Yes we get removed campgrounds.
So call this function if we are fresh.
We now have no campgrounds.
All right.
So let's clean this up and out a little comment here.
Remove all campgrounds then what we want to do next is add in a few campgrounds.
So add a few campgrounds and then we also want to add a few comments eventually.
Let's start with the campground.
So to create a campground we need to do a campground created.
And we need to pass in data.
But if we want to create a bunch of different campgrounds when I'm going to do is actually find some
data up here we'll just call it data.
And it's an array.
And in that array going to have a few objects and each object is going to be the starter data or the
data for one campground.
So we'll have name and we'll just do Cloud's rest again and then I'll get a new photo or use this one
for now.
And then that will be image and then format this a little bit nicer.
Let's put this on separate lines and then we need a description as well and our description will just
be blah blah blah.
OK so that's one piece of data and then I'm going to add in at least two more just like that and we'll
do one more now.
So we have three starter pieces of data.
I will change the images.
So let's take this one as an image
and we'll call this desert Mesa or something like that.
And then we'll add one more in and we'll go with this right here.
Copy the image or L and we'll call this canyon floor and I'll paste that image in and then we'll keep
the same description.
All right.
So what we've done so far we're not using this yet but we've to find an array that has three pieces
of data and each one is an object that has a name image and description which is exactly what our model
was expecting.
If we go over here model his name image and description.
OK.
So then what we need to do is actually loop through this data and create a campground for each one so
we can do it for each.
So I'll do that right now.
We'll do a data for each function and we'll just call it seed.
So that seed is going to represent one of these.
And then inside the loop we're going to do a campground or do you have it written here campgrounds create
and what we're going to pass in is not an object in curly braces to give it seed just like that.
And then our function here and this will have error and we'll also have data and we'll do it.
If there's an error cancel out log error else Council vote log and we'll just you added a campground
.
So we've created this array of starter data three different objects that we want to add to the database
and then we're looping through that and we're adding each one using campgrounds create.
So if we run this now if we restart the server we should see Yelp server had started up camp server
added a campground added campground added campground and then we get removed campgrounds and that might
not be what you're expecting.
But the reason that that's happening is really important.
This is why we use callbacks.
So remember when we run campgrounds remove there's no guarantee that this code is going to happen after
this finishes unless we put it inside the callback.
So we actually want to move all of this inside of this callback here right there campground out remove
and we'll just paste that in and we'll indent this just like that.
So what this will do now is wait until we remove all the existing campgrounds and then will add in our
new campgrounds.
All right.
So now we should see those in a different order and we get removed campground added added added.
And if we refresh we now see those three campgrounds.
OK.
So we have our sea data working now we want to work on comments and the way that I'm going to do this
is after we create a campground inside the else I'm going to create our comments on each campground
and we'll just do the same comment this time.
So what we want to do is run comments that create which doesn't exist yet it's not going to work and
that's fine we'll get an error.
And the comment that we're going to create we'll have a text of this place is great but I wish there
was Internet and let's form at this a little bit better.
So we're going to create this comment that has text and will have an author of Homer.
OK so this will create a comment and then we need our callback here function error and then the comment
that was created you can see this is getting out of hand quickly with all these comments.
You will see a way to refactor this soon.
This is what's called Colback hell.
If so many callbacks So this will run once the comment has created and once it is created we then want
to associate it with the campground and then we're going to grab the campground that was created which
we're currently calling data.
And rather than calling it data let's just call it campground and then down here we're going to do a
campground dot comments up push the new comment that we just created that has this text and this author
.
I don't need a comma here as well.
And then once we do that we need to save the campground and then we also add the error handling in here
so will add an if error.
So if we can't create a comment then we'll constantly log an error.
And if we can create a comment then we're going to associate it with the campground and we'll also do
cancel that log in here that says created new comments.
All right.
So this is a little bit of a marathon.
Let's let's go over some of this.
So what we're doing we're starting by removing everything in the database all campgrounds.
Technically we're not removing comments so we can come back and fix that once we have comments.
But we are moving all campgrounds and then we're creating three campgrounds.
Each one unique because of the seed data.
And then once we create one we're going to create a comment for that one and the comments are all the
same.
So we're going to end up with three different posts.
Three different campgrounds.
But when you click on them we'll see if they have the exact same comment that says This place is great
but I wish there was Internet written by Homer.
And then once we create that comment we then have to associate it with the campground push it into the
comments array on the campground and then save the campground and then we'll print out created new comment
and you'll see we have a little warning here.
Comment is not defined.
We'll need to add a VAR comment.
Equals require the non-existent dot slash models slash comment.
And this is what I mean by error driven development.
If I stopped the server and I restart I get a message that tells me I cannot find module models.
Slash comment in the next video we're going to focus on making these errors go away.
So what we've done is we set up a plan seed's file that works the way that we want it to work but it
doesn't quite work yet and we now need to make it work.