在我的app.js中,我需要方法覆盖以及app.use(methodOverride("_method"))
此
显示我的错误,我注意到错误不包括?_method = PUT就像网址中的内容一样。
这是我的ejs文件,链接到我的app.js
<% 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);
}
});
});
答案 0 :(得分:1)
您已经覆盖了方法类型,因此将其视为PUT请求。
您有router.post("/:id",
,但没有router.put("/:id",
。
由于没有与URL和方法匹配的路由,因此会出现错误。