Creating and Styling Comments for YelpCamp
This week I finally I got to the comments section of the tutorials. In this section we took what we learned about data association and use that to create comments made by users. I first created a seeding file which would delete and create new campgrounds every time we ran our app. This is done so it can show how the comments created through the app become referenced in the Campgrounds collection in our database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
var mongoose = require("mongoose"), Campground = require("./models/campground"), Comment = require("./models/comment"); var data = [ { name: "Cloud's Rest", image:"https://farm8.staticflickr.com/7205/7121863467_eb0aa64193.jpg", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin suscipit fringilla libero, aliquet lobortis sapien venenatis quis. Mauris viverra id nisi ac..." image:"https://farm6.staticflickr.com/5181/5641024448_04fefbb64d.jpg", }, { name: "Desert Mesa", image:"https://farm6.staticflickr.com/5181/5641024448_04fefbb64d.jpg", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin suscipit fringilla libero, aliquet lobortis sapien venenatis quis..." }, { name: "Canyon floor", image:"https://farm5.staticflickr.com/4153/4835814837_feef6f969b.jpg", description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin suscipit fringilla libero, aliquet lobortis sapien venenatis quis..." } ]; function seedDB(){ Campground.remove({}, function(err){ if (err){ console.log(err); }else{ console.log("removed campgrounds!"); } //add a few campgrounds data.forEach(function(seed){ Campground.create(seed, function(err, campground){ if(err){ console.log(err); }else{ console.log(campground); //create a comment Comment.create( { text:"This place is great, but I wish there was internet", author:"Homer" }, function(err, comment){ if (err){ console.log(err); } else{ campground.comments.push(comment); campground.save(); console.log(comment); } }); } }); }); }); //add a few comments } module.exports = seedDB; |
After creating our seed file I went over nested routes. Nested routes are routes that depend on IDs from objects in the database. Since I had to create a relation between the comments and the campgrounds, the comments needed the ID of the campground so that they will appear on the correct campground page. I also had to use the referencing data method to create this association.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
//================== //Comments Routes //================== app.get("/campgrounds/:id/comments/new", function(req, res){ //find campground by id Campground.findById(req.params.id, function(err, campground){ if (err){ console.log(err); } else{ res.render("comments/new", {campground: campground}); } }); }); app.post("/campgrounds/:id/comments", function(req,res){ //lookup campground using ID Campground.findById(req.params.id, function(err, campground){ if (err){ console.log(err); res.redirect("/campgrounds"); }else{ Comment.create(req.body.comment, function(err, comment){ if(err){ console.log(err); }else{ campground.comments.push(comment); campground.save(); res.redirect("/campgrounds/" + campground._id); } }); } }); }); |
Recent Comments