2
votes

Schema

var campgroundSchema = new mongoose.Schema({
     name: String,
     image: String,
     description: String,
     _id: String
});

var Campground = mongoose.model("Campground", campgroundSchema);

app.get("/campgrounds/:id",function(req , res){
    Campground.findById(req.params._id, function(err, foundCampgrounds){
        if(err){
            console.log(err);
        }else{
            res.render("show", {campground: foundCampgrounds});
        }
    });
});

show.ejs

<%- include('partials/header')  %> 
<h1>this is a show template</h1>
<p><%= campground.name %></p>
<img src="<%=campground.image%>">

<%- include('partials/footer')  %> 

error

TypeError: /home/ec2-user/environment/Yelpcamp/v2/views/show.ejs:3`
    1| <%- include('partials/header')  %> `
    2| <h1>this is a show template</h1>`
 >> 3| <p><%= campground.name %></p>`
    4| <img src="<%=campground.image%>">`
    5| 
    6| <%- include('partials/footer')  %>` 

Cannot read property 'name' of null at eval (/home/ec2-user/environment/Yelpcamp/v2/views/show.ejs:13:37) at show (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:656:17) at tryHandleCache (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:254:36) at View.exports.renderFile [as engine] (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:459:10) at View.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/view.js:135:8) at tryRender (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:640:10) at Function.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/response.js:1012:7) at /home/ec2-user/environment/Yelpcamp/v2/app.js:74:17 at /home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/model.js:4828:16 at /home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:4390:12 at model.Query.Query._completeOne (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2074:12) at Immediate.Query.base.findOne.call (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2136:10) at Immediate. (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mquery/lib/utils.js:116:16) at runCallback (timers.js:705:18) at tryOnImmediate (timers.js:676:5)

i am getting this error even after restarting mongodb and how can i check if the name is null or not

3
Are you sure you're getting a result from mongoose? It seems foundCampgrounds is null. can you console.log it? - i.brod
yeah it is showing that the foundCampgrounds is null ,but i can't understand why - Nitin Priyadarshi
Do req.params.id , not _ id - i.brod
Lol yelpcamps..is it from some udemy course? I remeber it - i.brod
thank you and yeah it is from udemy - Nitin Priyadarshi

3 Answers

1
votes

bruh it has to be req.params.id without underscores : )

0
votes

The solution is:

  1. After restarting the server, open the "/campgrounds" page, which in my case is "http://localhost:3000/campgrounds"
  2. Thereafter click on more info.

I also faced the same problem, but when I opened from /campgrounds page it was solved

0
votes

The solution of the problem is bit simple and it is in this line of code

res.redirect("/campgrounds"{campground:newCampgrounds})

You might be missing referencing the newCampgrounds to the campground that will be going to be used in the show.ejs as campground.name or campground.image so on..