Search code examples
node.jsbackbone.jsmongoose

ERR_CONNECTION_RESET upon Backbone.save() to update model by Mongoose


Please, tell me what I am doing wrong: My backbone view creates and saves document in mongodb by mongoose and uses the data in the view allright upon the backbone save() and fetch() methods. But when i use Backbone model.save({option: 'modified'}); with route.put() on the backend, jquery fires ERR_CONNECTION_RESET. I tried lots of things i found in the net, but they did not work. Maybe i need to use ajax.Prefilter or something of this kind, but i do not know exactly what to do.

the piece of code in backbone view firing update is:

this.user.save({ options: 'modified' }, {
    wait: true,
    success: function(model, response) {
        console.log('saved');
    },
    error: function(model, error) {
        console.log(error);
    }
});

in router index.js

router.put('/user/:id', require('./user').put);

it works because on GET route it works perfectly well.

in user.js

exports.put = function(req, res) {
    var query = { _id: req.params.id };
    User.update(query, { options: req.body.options }, function(){
        res.send('ok');
    });
};

I also experimented a lot with id or _id, and various ways of using mongoose update, like

User.findOne({ _id: req.params.id }, function (err, doc){
   if (err) return console.log(err);
   doc.options = req.body.options;
   doc.save(function(err){
       res.json(doc);
   });
});

and others such like. It did not work. Here is the error (it must be the key to my problem, but i cannot figer it out)

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

PUT http://localhost:3000/user/56349be42b19125405c2d66a net::ERR_CONNECTION_RESET


Solution

  • It was silly: maximum req size exceeded. Cured by setting

    app.use(bodyParser.json({limit: '16mb'})); 
    app.use(bodyParser.urlencoded({ limit: '16mb', extended: true }));