【发布时间】:2018-08-02 19:20:20
【问题描述】:
在我的 app.js 中,我需要方法覆盖和 app.use(methodOverride("_method"))
这个
显示我的错误,我注意到该错误不包括 ?_method=PUT 像 url 中的内容。
这是链接到我的 app.js 的我的 ejs 文件
<% include ../partials/header %>
<div class = "container">
<div class="row">
<h1 style = "text-align: center;">Edit <%= campground.name %></h1>
<div style = "width: 30%; margin: 25px auto;">
<form method = "POST" action ="/campgrounds/<%= campground._id %>?_method=PUT">
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.name %>" name = "campground[name]">
</div>
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.image %>" name = "campground[image]">
</div>
<div class="form-group">
<input class = "form-control" type = "text" value="<%= campground.description %>" name = "campground[description]">
</div>
<div class="form-group">
<button class="btn btn-lg btn-default btn-primary btn-block">Submit!</button>
</div>
</form>
<a href="/campgrounds/<%= campground._id %>">Go back</a>
</div>
</div>
</div>
<% include ../partials/footer %>
这是我的编辑/更新路线
//Edit campground route
router.get("/:id/edit", function(req, res){
Campground.findById(req.params.id, function(err, foundCampground){
if(err){
res.redirect("/campgrounds");
} else{
res.render("campgrounds/edit", {campground: foundCampground});
}
});
});
//Update campground route
router.post("/:id", function(req, res){
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCampground){
if(err){
res.redirect("/campgrounds");
} else{
res.redirect("/campgrounds/" + req.params.id);
}
});
});
【问题讨论】:
-
“我注意到错误不包括 ?_method=PUT 就像 url 中的内容一样” - 这没关系。很明显,它已被识别,因为错误消息说它正在尝试 PUT 数据。
标签: javascript node.js express web-development-server