More Seeds File

So now comes the slightly more exciting bit (I’m lame, I know!), we are going to start creating comments!!

Again, we’re working in reverse order here. Starting by creating comments in the seeds file and then adding in the functionality to app.js.

In seed.js I have added even more callbacks.. creating a pretty gross mess! But Colt (the course instructor) assures me that we will be tackling that disaster in another video.

This is what we’re looking at:

seeds2

You might notice that there is a warning symbol on line 36. This is because have to yet to define Comment. This is the first example of error driven development. There is an error and now I need to address it to make my code function. I have to require Comment in the same way that I required Campground.

var Comment = require("./models/comment");

The file comment does not exist yet and that will be the next error I address.

err1

So this one is an easy fix! Create the comment.js file in the models directory. Inside of that file create the comment schema and return the model. Done and done! When I restart the server the error message has been replace with a new one! Woo! New problem to solve! The error I am receiving now is

TypeError: Cannot read property 'push' of undefined

This one is extra exciting because Colt didn’t get it – which means I get to do some problem solving on my own. Generally the first thing I do when I have unexpected error is read the message and think about what it could mean.. Duh, right?.. So in this case I know where I used push – adding a comment to the comment array and I know what I was pushing to – the campground object. So why is it undefined? I dunno! Next step is to read teh code over and see if anything jumps out at me. Nothing looks off, and it matches Colt’s code. So now I’ll check my database and see what’s going on there.

To check the database I need to open another terminal window and type mongo. From there I can see what databases I have and what collections I have within each database. It looks like I have a collection for comments in my yelp_camp database and it has the three comments I expect. So my code is working, awesome! But why the error? Time to do some more digging!

Before heading to google and stack overflow I usually go back through the course notes from the TA. These are helpful updates, additions and notes to address any changes or bugs in the course work. Sure enough I found the note about this issue and it looks like I jumped the gun a little and it’s about to be addressed in the video. I gather the issue came from pushing to comments without a comments array predefined in the camgroundSchema.

So now we will add the comments property to the campground model, just like in the post about associations – specifically references.

var mongoose = require("mongoose");

var campgroundSchema = new mongoose.Schema({
    name: String,
    image: String,
    description: String,
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }]
});

module.exports = mongoose.model("Campground", campgroundSchema);

So after all that I was still getting the same error.. what the heck right? So I combed through my code again and look harder. Turns out I hadn’t changed one of the values in one of the callback funtions to campgrounds – so I was calling push on the wrong value.

If you look online 29 on the screeen shot above you will see that my callback function takes err and data, but then I call Campground.comments.push(comment); and Campground.save()
It should have been data.comments.push(comment) and data.save()
But thats not super clear so I replaced data with campground and BAM!! we have lift off!!

Usually when I run into issues it’s something simple.. often at the end of many hours of chugging away at a project.

That’s all for my seeds.js posts! Look forward to a post about problem solving on my own and more on Yelp Camp!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: