Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoose model.save() not persisting to database on update
    text
    copied!<p>I am developing a mongoose / node.js / express app.</p> <p>in my routes I am using <a href="http://expressjs.com/api.html#app.param" rel="nofollow">express' app.param()</a> method to get my model instance into the request - instead of fetching it in each controller action.</p> <p>i got <code>show</code> and <code>create</code> actions working in my controller - however I am stuck implementing the <code>update</code> action.</p> <p>here is my relevant controller code:</p> <pre><code>// mymodel-controller.js var mongoose = require('mongoose') var MyModel = mongoose.model('MyModel'); var utils = require('../../lib/utils'); "use strict"; exports.update = function (req, res, next) { var mymodel = req.mymodel; mymodel.set(req.body); mymodel.slug = utils.convertToSlug(req.body.title); mymodel.save(function(err, doc) { if (!err) { console.log('update successful'); // here i see the correctly updated model in the console, however the db is not updated console.dir(doc); return res.redirect('/mymodels/' + mymodel.slug); } else { console.error('update error', err); return res.render('mymodels/edit', { title: 'Edit Model', model: mymodel, errors: err.errors }); } }); } </code></pre> <p>The strange thing is, the mongoose save goes through successfully, I don't get any error. I see 'update successful' and the correctly updated model printed on the console, but not persisted in the database. I tried also fetching the model manually before updating and saving it, instead of using app.param(..) but I had the same effect. Any idea what I am missing here?</p> <p><strong>update</strong></p> <p>this is the code where I set the <code>req.mymodel</code> - part of a longer routes file. In my show actions e.g. I also use req.mymodel to display it and it works fine so far.</p> <pre><code>/** * mymodel ROUTES */ app.param('mymodel', function(req, res, next, id){ MyModel.findOne({ 'slug': id }, function(err, mymodel) { if (err) { next(err); } else if (mymodel) { req.mymodel = mymodel; next(); } else { res.status(404).render('404', {title: "Not found", errorMessage: "The requested mymodel was not found."}); } }); }); app.get('/mymodels', mymodelsController.index); app.get('/mymodels/new', mymodelsController.new); app.post('/mymodels', mymodelsController.create); app.get('/mymodels/:mymodel', mymodelsController.show); app.get('/mymodels/:mymodel/edit', mymodelsController.edit); app.put('/mymodels/:mymodel', mymodelsController.update); // that's the one not working with the code above app.del('/mymodels/:mymodel', mymodelsController.destroy); </code></pre> <p><strong>update2</strong></p> <p>This code, on the other hand, works in my controller and updates the database (I'd much prefer using instance.save() though)</p> <pre><code>exports.update = function (req, res, next) { var mymodel = req.param('mymodel'); var query = { slug: mymodel }; var update = {}; update.title = req.param('title'); update.body = req.param('body'); update.points = req.param('points'); update.location = req.param('location'); update.slug = utils.convertToSlug(req.param('title')); MyModel.update(query, update, function (err, numAffected) { if (err) return next(err); if (0 === numAffected) { return next(new Error('no model to modify'), null); } res.redirect('/mymodels/' + update.slug); }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload