Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update model with Mongoose, Express, NodeJS

I'm trying to update an instantiated model ('Place' - I know it works from other routes) in a MongoDB and have spent a while trying to properly do so. I'm also trying to redirect back to the page that views the 'place' to view the updated properties.

Node v0.4.0, Express v1.0.7, Mongoose 1.10.0

Schema:

var PlaceSchema = new Schema({
name  :String
,  capital: String
,  continent: String
});

Controller/route:

app.put('/places/:name', function(req, res) {
var name = req.body.name;
var capital = req.body.capital;
var continent = req.body.continent;
Place.update({ name: name, capital: capital, continent: continent}, function(name) {
    res.redirect('/places/'+name)
});

});

I've tried a bunch of different ways but can't seem to get it.
Also, isn't how I declare the three {name, capital, and continent} variables blocking further operations? Thanks. General debugging help is also appreciated. Console.log(name) (right below the declaration) doesn't log anything.

Jade form:

h1 Editing #{place.name}
form(action='/places/'+place.name, method='POST')
  input(type='hidden', name='_method', value='PUT')
  p
    label(for='place_name') Name:
    p
    input(type='text', id='place_name', name='place[name]', value=place.name)
    p
    label(for='place_capital') Capital: 
    p
    input(type='text', id='place_capital', name='place[capital]', value=place.capital)
    p
    label(for='place_continent') Continent:
    p
    textarea(type='text', id='place_continent', name='place[continent]')=place.continent
    p
    input(type="submit")
like image 649
JohnAllen Avatar asked Feb 17 '11 03:02

JohnAllen


3 Answers

You have to find the document before updating anything:

Place.findById(req.params.id, function(err, p) {
  if (!p)
    return next(new Error('Could not load Document'));
  else {
    // do your updates here
    p.modified = new Date();

    p.save(function(err) {
      if (err)
        console.log('error')
      else
        console.log('success')
    });
  }
});

works for me in production code using the same setup you have. Instead of findById you can use any other find method provided by mongoose. Just make sure you fetch the document before updating it.

like image 154
schaermu Avatar answered Nov 16 '22 23:11

schaermu


Now, i think you can do this :

Place.findOneAndUpdate({name:req.params.name}, req.body, function (err, place) {
  res.send(place);
});

You can find by id too :

Place.findOneAndUpdate({_id:req.params.id}, req.body, function (err, place) {
  res.send(place);
});
like image 28
k33g_org Avatar answered Nov 17 '22 01:11

k33g_org


So now you can find and update directly by id, this is for Mongoose v4

Place.findByIdAndUpdate(req.params.id, req.body, function (err, place) {
  res.send(place);
});

Just to mention, if you needs updated object then you need to pass {new: true} like

Place.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, place) {
  res.send(place);
});
like image 31
Abhishek Gupta Avatar answered Nov 16 '22 23:11

Abhishek Gupta