// 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:-
https://github.com/160303105370/Error
**
