我正在尝试传递注释并将注释(带有名称和作者的对象)与露营地对象相关联。但是当我尝试通过发布路线传递它时,它将显示未定义。
此处发生错误
//add a new comment in campground
app.post('/campgrounds/:id/comments',function(req,res){
//find campground with id
Campground.findById(req.params.id,function(err,campground){
if(err){
console.log(err)
res.redirect('/campgrounds/');
}else{
console.log(req.body.comments)
}
})
//create comment
//associate created comment to campground
//redirect to show page
})
这是评论表
<%-include('../partials/header')-%>
<div class="container">
<div class="row">
<div style="width: 40%; margin: 50px auto;">
<h1 style="text-align: center;">Add a new comment to <%=campground.name%> </h1>
<form action="/campgrounds/<%-campground._id %>/comments" method="POST">
<div class="form-group">
<input type="text" class="form-control" name="comments[text]" placeholder='Text'>
</div>
<div class="form-group">
<input type="text" class="form-control" name="comments[author]" placeholder="Author">
</div>
<button class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<%-include('../partials/footer')-%>
结果显示为未定义,尽管先前的发布路线可以正常工作。这不是body解析器.help
的问题