Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js/ express.js/ Backbone.js : req.body.keys is undefined?

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...

Problem courtesy of: Petrov

Solution

The syntax for keys is different:

Object.keys(req.body).forEach(function(key){ ... })
Solution courtesy of: evilcelery

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js/ express.js/ Backbone.js : req.body.keys is undefined?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×