【发布时间】:2020-04-25 12:41:02
【问题描述】:
这是 Colt Steele 的 yelp camp 项目。我一直在创建 cmets,我总是收到此警告 Cannot read property 'name' of null。 这是我的 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') %>
有人可以帮我看看我的代码有什么问题吗?如果我签入数据库,露营地已经有数据了
【问题讨论】:
-
console.log(foundCampground)的结果是什么? -
foundCampground的输出是什么? -
它说它是未定义的
-
露营地是在同一个文件夹中还是在名为 campgrounds 的子文件夹中?
标签: javascript node.js mongodb associations ejs