Node.js/ express.js/ Backbone.js : req.body.keys is undefined?
Problem
I'm using Backbone.js on the client side and express.js on the server, and I'm trying to save/put a model update via Backbone. I've done nothing to Backbone.sync, and so it should be just plain old $.ajax.
On the server, I've got a simple
app.put('my-url', function(req, res){
req.body.keys.forEach( function(key){
// do stuff with key
});
});
Each time, I get an error message saying 'cannot call method 'forEach' of Undefined, and sure enough a quick console.log shows that req.body.keys is undefined. Another quick couple of console.logs show that Object.keys and 'forEach' are just fine, that typeof req.body is object.
Anyone see what is going wrong? I tried JSON.parse(req.body) and got the famous 'unexpected token o' error...
Solution
The syntax for keys
is different:
Object.keys(req.body).forEach(function(key){ ... })
Discussion
View additional discussion.