Nodejs: Cannot read property 'name' of null

// File that I imported

‘’’
var express = require(‘express’),
app = express(),
bodyPaser = require(‘body-parser’),
mongoose = require(‘mongoose’),
Comment = require(’./models/comment’),
Campground = require(’./models/campground’),
seedDB = require(’./seeds’);
var Campground=require(’./models/campground’);
mongoose.connect(‘mongodb://localhost/yelp_camp_v3’,{useNewUrlParser: true });
‘’’

//Schema for campground
‘’’
var mongoose=require(‘mongoose’);
var campgroundSchemma=new mongoose.Schema({
name: String,
image: String,
description: String,
comments:[{
type: mongoose.Schema.Types.ObjectId,
ref:‘Comment’
}]
});

module.exports=mongoose.model(‘Campground’,campgroundSchemma);
‘’’

//Schema for comment
‘’’
var mongoose=require(‘mongoose’);
var commentSchema=mongoose.Schema({
text:String,
author:String
});
module.exports=mongoose.model(‘Comment’,commentSchema);
‘’’

//Function seedDB which deletes existing campground and adds new campground

‘’’
function seedDB(){
Campground.remove({},function(err){
if(err){
console.log(err);
}
console.log(‘removed campground!’);
data.forEach(element => {
Campground.create(element,function(err,campground){
if(err){
console.log(err);
}
else{
console.log(‘added a campground!’);
//Create comment
Comment.create({
text:‘this place is gr8 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(‘Created a new comment’);
}
});
}
});
});

});

}
module.exports=seedDB;
‘’’

//Now, here I have connected campground and comments in a show page

‘’’
app.get(’/campgrounds/:id’,function(req,res){ Campground.findById(req.params.id).populate(‘comments’).exec(function(err,foundCampground){
if(err){
console.log(err);
}
else
{ console.log(foundCampground);
res.render(‘campgrounds/show’,{campground:foundCampground});
}
});
});
‘’’

//This is the show page that displays campground and comments
‘’’
<% include …\partials\header %>

<%=campground.name%>

<%=campground.description%>

Comments


<%campground.comments.forEach(function(comment){%>

<%=comment.author%>-<%=comment.text%>

<% })%> <% include ..\partials\footer %> '''

//ERROR, which is killing me

TypeError: C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\views\comment\new.ejs:3
1| <% include …\partials\header %>

2|     <div class='container'>

3|

Add a comment to <%=campground.name%>

4|     

5|         <div class='row'>

6|             <div style='width:30%; margin:25px auto;'>

Cannot read property ‘name’ of null
at eval (eval at compile (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\ejs\lib\ejs.js:618:12), :22:36)
at returnedFn (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\ejs\lib\ejs.js:482:10)
at View.render (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\express\lib\view.js:135:8)
at tryRender (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\express\lib\response.js:1012:7)
at C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\app.js:80:13
at C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\mongoose\lib\model.js:4753:16
at C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\mongoose\lib\query.js:4251:12
at model.Query.Query._completeOne (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\mongoose\lib\query.js:1972:12)
at Immediate.Query.base.findOne.call (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\mongoose\lib\query.js:2032:10)
at Immediate._onImmediate (C:\Users\Prashant\Desktop\website\Bootcamp\YelpCamp\v4\node_modules\mquery\lib\utils.js:116:16)
at runCallback (timers.js:810:20)
at tryOnImmediate (timers.js:768:5)

**If you want the whole program then here is the github link:-


**

I am a novice so I was trying to read your code to understand. Can you wrap your code in back ticks so we can see it better?

https://github.com/160303105370/Error this is the whole code so try and help if you could.

I am getting different error messages when I run your program:

Error: Could not find the include file "partials\header"
    at getIncludePath (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:162:13)
    at includeSource (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:306:17)
    at /Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:702:26
    at Array.forEach (<anonymous>)
    at Template.generateSource (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:678:15)
    at Template.compile (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:557:12)
    at Object.compile (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:389:16)
    at handleCache (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:212:18)
    at tryHandleCache (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:251:16)
    at View.exports.renderFile [as engine] (/Users/Sidney/Desktop/Error-master/node_modules/ejs/lib/ejs.js:482:10)
    at View.render (/Users/Sidney/Desktop/Error-master/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/Sidney/Desktop/Error-master/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/Sidney/Desktop/Error-master/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/Sidney/Desktop/Error-master/node_modules/express/lib/response.js:1012:7)
    at /Users/Sidney/Desktop/Error-master/app.js:19:9
    at Layer.handle [as handle_request] (/Users/Sidney/Desktop/Error-master/node_modules/express/lib/router/layer.js:95:5)

Which may have nothing to do with your original problem. But in your landing.ejs file, instead of:

<% include partials\header %>

try:

<% include partials/header %>

Same for footer. Sorry if this is not helping! But when I ran your program, and changed the \ to / it worked?

Thanks for the answer but ‘’ or '/ ’ are related to path in different operating systems.For reference:

But, really thanks for reaching out as you are the only one till now who has reached to me for help. So, thanks and reply to me again if u get it.

// bad
export default function () {

}

// good
export default function MyName () {

}
For more code related information visit https://github.com/standard-things/esm/issues/434 and for more further issues visit Data Recovery Dubai Group

Hey I’m stuck at the same situation… Did you find any solution ?

Hi @omakr_k45, I happen to be working on a similar project. you can try this: Load the server in terminal, refresh your webpage twice. I hope this helps :slight_smile:

@Prashnat am also facing the same issue .it is working for the first time but after that it starts throwing error .
can u please help me resolve this issue ,i am unable to procceed forward because of this