Type Error : Cannot read property ‘name’ of null [closed]

Tags: , , , ,



i am having a problem with my nodejs server connected to mongoDB

this is the main code for app.js

var express = require("express")
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose")

mongoose.connect("mongodb://localhost:27017/yelpcamp", {useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");

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

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



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

app.get("/campgrounds", function(req,res){
    Campground.find({}, function(err, allcampgrounds){
        if(err){
            console.log()
        } else {
            res.render("index", {campgrounds:allcampgrounds});
        }
    })
    
})

app.post("/campgrounds", function(req,res){
    var name = req.body.name;
    var image = req.body.image;
    var newCampground = {name : name, image: image}
    Campground.create(newCampground, function(err, newlyCreated){
        if(err){
            console.log(err)
        } else {
            res.redirect("/campgrounds")
        }
    })
    
})

app.get("/campgrounds/new", function(req,res){
    res.render("new.ejs")
})

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

app.listen(3000, function(){
    console.log("The Server has started listening on Port 3000!")
})

That is my main routes and the problem i am having is using the last route for the /campgrounds/:id on the show page i am just trying to call

and it just returns this error:

TypeError: /mnt/c/Users/zhelf/Downloads/test/yelpcamp/views/show.ejs:4 2| 3|

4|

<%= campground.name %>

Cannot read property ‘name’ of null at eval (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/views/show.ejs:10:37) at show (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:691:17) at tryHandleCache (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:272:36) at View.exports.renderFile [as engine] (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:489:10) at View.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/view.js:135:8) at tryRender (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/application.js:640:10) at Function.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/response.js:1012:7) at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/app.js:85:17 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4824:16 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4824:16 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4847:21 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/query.js:4390:11 at /mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/kareem/index.js:135:16 at processTicksAndRejections (internal/process/task_queues.js:79:11)

Answer

change Campground.findById(req.params._id, function(err, foundCampground)

to Campground.findById(req.params.id, function(err, foundCampground)

as you need to provide id coming from route “/campgrounds/:id”



Source: stackoverflow