Search code examples
javascriptnode.jsbackbone.jsrestify

Backbone - Undefined request body


This must be something I do not understand properly.

I have a Backbone model named Album:

var Album = Backbone.Model.extend({
idAttribute: "_id",
urlRoot: "http://localhost:8000/albums",
defaults:{
    "_id": null,
    "title": "",
    "year": "",
    "genre": ""
    }
});

and I have a Node.js server where I use Restify:

var server = restify.createServer();
server.put('/albums/:id', putAlbum); //putAlbum is a function defined above.
server.use(restify.bodyParser({ mapParams: false }));

My problem is that when I save an existing Album model (and also when i post a new one with the create method from the collection), the server receives the request and calls right function, but when I do this:

console.log(req.body);

the body is 'undefined'. I always tried to check the params attribute but it only had the id in it. Is it something I misconfigured ?


Solution

  • Reorder your code to parse the body before your route handler is called:

    var server = restify.createServer();
    server.use(restify.bodyParser({ mapParams: false }));
    server.put('/albums/:id', putAlbum);