Method-Override put request

In my app.js, I required method-override and also app.use(methodOverride("_method"))

This

shows my error, what i've noticed is that the error does not include the ?_method=PUT like what's in the url.

This is my ejs file that is linked to my 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 %>

And this is my edit/update routes

//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);
      }
   }); 
});

Answers:

Answer

You've overridden the method type, so it is being treated as a PUT request.

You have router.post("/:id", but not router.put("/:id",.

Since you have no route that matches the URL and the method, you get the error.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.