2

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

MaartenDev
  • 5,631
  • 5
  • 21
  • 33

3 Answers3

1

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

  • 4
    Good answer but please provide a more professional oriented answer. Use examples and documentation sources to harden your point. Specifically more on the topic of request and params with different examples. – xIsra Oct 22 '20 at 19:57
  • please add some explanation to the answer – Zig Razor Oct 23 '20 at 06:47
0

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

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..

Todd
  • 652
  • 2
  • 19
  • 37