0

I'm making a simple website with an api using node, express, and mongo with a simple edgejs for front-end. All routes are working in postman (GET, POST, PUT, DELETE). However, HTML5 does not support the PUT method. Here is one example.

Index.js

... 
app.put('/api/item/:id', function(req, res, next){
      About.findByIdAndUpdate({_id: req.params.id}, req.body)
      .then(function(){
        About.findOne({_id: req.params.id})
        .then(function(about){
          res.send(about)
        });
      }).catch(next)
    });

This works fine in postman. Here's what I initially had in my view.

edititem.edge

    ...
    <form 
     action="/api/item/{{item._id}}" 
      method="PUT"
      encType="multipart/form-data">
        <div class="form-group">
          <label for="title">Name</label>
          <input type="text" class="form-control" name="name" placeholder="Name">
        </div>
        <div class="form-group">
          <label for="description">Profession</label>
          <input type="text" class="form-control" name="profession" placeholder="Profession">
        </div>
        <div class="form-group">
          <label for="description">Description</label>
          <input type="text" class="form-control" name="description" placeholder="Description">
        </div>
        <div class="form-group">
          <label for="description">Email</label>
          <input type="text" class="form-control" name="footerEmail" placeholder="Email">
        </div>
        <div class="form-group">
          <label for="content">Footer Description</label>
          <textarea name="footerDescription" id="" class="form-control" rows="10" placeholder="Footer Description"></textarea>
        </div>
        <div class="form-group text-center">
            <button class="btn btn-primary">Edit Item</button>
          </div>
      </form>

I considered binding a function to the edit button to just delete the previous item and create a new one with the updated content, but that can't be best practice, can it? That would change the id, and from what I've seen in Postman, a proper PUT doesn't change the id. My idea to delete and replace would.

So how do I allow this item to be edited from the front end?

Aaron Toliver
  • 187
  • 1
  • 3
  • 16
  • Refer to this - https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms – xan_z Oct 26 '18 at 02:08
  • I appreciate the reference, xan, but in my search to find a work-around, I had already found that article. It explains why HTML5 does not support PUT or DELETE, and I understand it. But how do I work around these restrictions? There are plenty of applications that allow the user to edit and delete their data, at least as far as the user can see. How is it done? I've looked in every place I can think of. – Aaron Toliver Oct 26 '18 at 04:11
  • your front end app - angular/Jquery or JS should help you with that. That is what I use. – xan_z Oct 26 '18 at 14:10

0 Answers0