0

I am trying to allow users to update existing mongoose documents using a form with method-override package. I have read the other Stackoverflow posts on the issue, but none of them seem to resolve my problem. I want the user to be able to see their existing document they created, submit the form with new values, and have it update the database.

I have tried the solution provided here How do I update/upsert a document in Mongoose? among others and it hasn't worked.

I have setup this initial route to show the individual document:

// EDIT - edit location route
router.get("/location/:id/edit", function(req, res) {
    Location.findById(req.params.id, function(err, foundLocation){
        res.render("./locations/edit", {location: foundLocation});
    });
});

Which leads them to a landing page with this form to update the information on that document

<form action="/location/<%= location._id %>?_method=PUT" method="POST">
     <div class="form-group"><input class="form-control" type="text" name="location[name]" ></div>
     <div class="form-group"><input class="form-control" type="text" name="location[image]" value="<%= location.image %>"></div>
      <div class="form-group"><input class="form-control" type="text" name="location[longDescription]" value="<%= location.longDescription %>"></div>
       <div class="form-group"><button class="btn btn-lg btn-primary btn-block">Submit!</button></div>
</form>

The route that handles that PUT logic is:

router.put("/location/:id", function(req, res) {
    Location.findByIdAndUpdate(req.params.id, req.body.location, {new: true}, function(err, updatedLocation){
        console.log(req.body.location);
        if(err) {
            console.log(err);
            res.redirect("/search");
        } else {
            console.log(updatedLocation);
            res.redirect("/location/" + req.params.id);
        }
    });
});

The issue I'm having is this route isn't firing any errors, but it also isn't updating the document. I've tried to console.log the req.body.location and it is returning undefined. I feel that may be the issue but I'm not sure how to fix it

@Deda this is what is logged in console.log(req.body)

  'location[image]':
   'https://wekivaisland.com/wp-content/uploads/bartlettimage-4977.jpg',
  'location[longDescription]': 'Edited Long Description' }
{ address:
   { street: '51 SW 11th Street Apt. 1537',
     city: 'Miami',
     state: 'FL',
     zip: '12345',
     country: 'United States' },
  author: { id: 5d081e0afd38374b43ca0c14, username: 'danny' },
  rating: 0,
  _id: 5d08dd702456a30948947c73,
  name: 'Random Place',
  image:
   'https://2.bp.blogspot.com/-yKPXCFHoNhQ/WPOOwjqA3QI/AAAAAAAABmQ/88DcGs-Cp5oXAv6fA6hn3J7NRUlYBaxgwCLcB/s1600/Screen%2BShot%2B2017-04-16%2Bat%2B11.33.00%2BAM.png',
  longDescription: 'Place #2',
  shortDescription: 'Place #2',
  __v: 0 }```


This has been resolved. I just needed to set bodyParser extended to true.

It has previously read

```app.use(bodyParser.urlencoded({ extended: false }));``

by changing this to the code below everything worked properly.

```app.use(bodyParser.urlencoded({ extended: true }));```

1 Answers1

0

Can we see the request you are trying to fire off to your API? What tool are you using to debug? If req.body.location is undefined, that would definitely be an issue :)

J. K.
  • 41
  • 7
  • I am posting the following form action using the method-override package to allow me to use the PUT method. ```
    ``` I am then using Model.findByIdAndUpdate within my PUT route as seen here `router.put("/location/:id", function(req, res) { Location.findByIdAndUpdate(req.params.id, req.body.location, {new: true}, function(err, updatedLocation){` but the req.body.location is coming through blank and I can't figure out why. I'm only using node.js callbacks to track errors. I'm open to suggestions.
    – Daniel Lambert Jun 18 '19 at 17:17
  • Given your console log for `req.body`, it looks like you are very close. You see that you have the data you need in there, what is missing? – J. K. Jun 19 '19 at 13:58
  • Also, I would recommend learning to use a tool like Postman to test your APIs: https://www.getpostman.com/ – J. K. Jun 19 '19 at 14:15
  • I was actually able to find the resolution and it was the settings of bodyParser. I've included it in the question. Thanks for your help! – Daniel Lambert Jun 20 '19 at 20:06