bardu
bardu

Reputation: 737

Backbone.js model.save() - proper server response

Something seems to be wrong, either my server response or my save function. In neither case success nor error function of model.save() gets invoked.

My server response:

exports.updateBusinessBannerAd = function(req, res) {
// update banner ad
db.insert(req.body, req.body._id, function(err, data) {
    if (err) {
        console.log('Update business banner ad: ' + err.message + '   ' + err['status-code']);
        res.send(err['status-code'], { header: 'Update banner ad', message: err.message, type: 'error'});
    }
    else {
        res.send(200);
    }
}); 

};

And here my client code:

saveBannerAd: function(self) {
        self.model.id = self.model.attributes._id; 
        self.model.save({
            success: function(model, res) {
                    utils.growl( 'Banner Ad', 'has been updated!', 'info');
                    self.resetHandler();
            },
            error: function(model, res) {
                console.log(res);
                utils.growl(res.header, res.message, res.type);
                return;
            }    
        }); 
    },

What am I missing?

Upvotes: 4

Views: 4198

Answers (1)

bardu
bardu

Reputation: 737

Okay, adding { id: self.model.get('id') } as first argument to model.save() fixed my issue.

In this thread

someone suggested setting 'null' as first argument serves as placeholder, but my experience is that this will invoke the error function.

Upvotes: 1

Related Questions