【发布时间】:2019-12-24 17:34:55
【问题描述】:
当我提交表单时,我不断收到以下信息:
errorCastError: 路径中值“comment”的转换为 ObjectId 失败 "cmets"
我希望你的问题是正确的
这是req的形式
action="/campground/<%= camp._id %>/comments/" method="POST">
这是渲染表单页面的路径
app.get("/campground/:id/comments/new", function(req, res){
Campground.findById(req.params.id, function(err, camp){
if(err){
console.log(err);
} else {
res.render("comments/new", {camp: camp});
}
});
});
这是创建评论并将其与露营地相关联的发布路线
app.post("/campground/:id/comments", function(req, res){
Campground.findById(req.params.id, function(err, camp){
if(err){
console.log(err);
res.redirect("/");
} else {
Comment.create(req.body.comment, function(err, comment){
if(err){
console.log(err);
} else {
camp.comments.push("comment");
camp.save();
res.redirect("/campground/" + req.params.id);
}
});
}
});
});
应用正在监听这个
app.listen(process.env.PORT, process.env.IP);
【问题讨论】: