[在javascript中,我已将该对象传递给ejs文件,但仍然说该对象为null

问题描述 投票:0回答:1

这是Colt Steele的yelp营地项目。我被困在创建评论中,我总是得到这个警告无法读取null的属性'name']]。这是我的app.js文件

app.get('/campgrounds/:id', function(req, res){
// 	find campground with the provided ID
	Campground.findById(req.params.id).populate('comments').exec(function(err, foundCampground){
		if(err){
			console.log(err);
		} else {
			console.log(foundCampground)
			res.render('show', {campground: foundCampground});
		}
	});
});

这是show.ejs文件

<%- include('partials/header') %>
<h1>
    <%= campground.name %>
</h1>
<img src='<%= campground.image %>'>
<p><%= campground.description %></p>

<% campground.comments.forEach(function(comment){ %>
    <p><%= comment.author %> - <%= comment.text %></p>
<% })%>
<%- include('partials/footer') %>

有人可以帮我解决我的代码有什么问题吗?如果我在数据库中签到,则营地已经有数据了]

这是Colt Steele的yelp营地项目。我一直无法创建评论,但总是收到此警告。无法读取null的属性“名称”。这是我的app.js文件app.get('/ campgrounds /:id',...

javascript node.js mongodb associations ejs
1个回答
0
投票

[campground.comments json数据可能有问题。

© www.soinside.com 2019 - 2024. All rights reserved.