简体   繁体   中英

How can I update a document in Mongoose?

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM