Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (120 points)

Hi all,
I am trying to push a comment but when i submit it I get an error by being redirected to the /campgrounds Cast to ObjectId failed for value " 5e634e71e6ac7a04f90b00fe" at path "_id"

for model "Campground"',
 name: 'CastError', 
 model: Model { Campground } }page as the code below specifies. I don't get what I am doing wrong, I get the following error:

May someone let me know what I am missing? Thanks a lot in advance! I have the following Javascript file:

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
Campground = require("./models/campground");
Comment = require("./models/comment");
seedDB = require("./seeds");


mongoose.connect("mongodb://localhost/yelp_camp_v3", { useNewUrlParser: true });
mongoose.set("useUnifiedTopology", true);

app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
seedDB();

var campgrounds = [
        {name: "Mountain Goat's Rest", image:"?auto=compress&cs=tinysrgb&h=650&w=940"}
    ];

app.get("/", function(req, res){
    res.render("landing");
});

//INDEX - show all campgrounds
app.get("/campgrounds", function(req, res){
    //Get all campgrounds from DB
    Campground.find({}, function(err, allCampgrounds){
        if(err){
            console.log(err);
        } else{
          res.render("campgrounds/index", {campgrounds: allCampgrounds});
        }
    });

});

// CREATE - add new campground to db
app.post("/campgrounds", function(req, res){
    //get data from form and add to campground array
    var name = req.body.name;
    var image = req.body.image;
    var desc = req.body.description;
    var newCampground = {name: name, image: image, description: desc}
    //Create a new campground sna dsave to database
    Campground.create(newCampground, function(err, newlyCreated){
        if(err){
            console.log(err);
        } else {
            //redirect back to campgrounds
            res.redirect("/campgrounds");
        }
    });
});

// //CREATE NEW CAMPGROUND TO DELET IF NOTWORK
// app.post("/campgrounds/:id", function(req, res) {
//  Campground.create(req.body, function (err, newlyCreated) {
//      if (err) console.log(err);  else 
//      res.redirect('/campgrounds/' + newlyCreated._id);
//    })
// });


//NEW - show form to create new campground
app.get("/campgrounds/new", function(req, res){
    res.render("campgrounds/new");
});

//SHOW Show info about one campsite
app.get("/campgrounds/:id", function(req, res){
    //Find the campground with provided ID
    Campground.findById(req.params.id).populate("comments").exec (function(err, foundCampground){
        if(err){
            console.log(err);
        } else {
            console.log(foundCampground);
            //render show template with that campground
            res.render("campgrounds/show", {campground: foundCampground});
        }
    });

});

//--------------
// COMMENT 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);
                }
            });

        }
    });
    //create a new comments
    //connect new comment to campground
    //redirect  to campground show page
})


app.listen(3000, function() { 
  console.log('Yelpcamp Server Has Started'); 
});
And new.ejs file when i specify a post request:
<%- include("../partials/header") %> <div class="container"> <div class="row"> <h1 style="text-align: center;">Add New Comment to <%= campground.name %></h1> <div style="width: 30%; margin: 25px auto;"> <form action="/campgrounds/ <%= campground._id %>/comments " method="POST"> <div class="form-group"> <input class="form-control" type="comment[text]" name= "text" placeholder="text"> </div> <div class="form-group"> <input class="form-control" type="text" name = "comment[author]" placeholder="author"> </div> <div class="form-group"> <button class="btn btn-lg btn-primary btn-block">Submit!</button> </div> </form> <a href="/campgrounds"> Go Back</a> </div> </div>

Please log in or register to answer this question.

Related questions

Browse Categories

...