I have a form that looks more or less like this:
<input type="checkbox" name="checklist[1]" value="1" />
<input type="checkbox" name="checklist[2]" value="1" />
<input type="checkbox" name="checklist[3]" value="1" />
I was expecting that request.body.checklist
looked like this (after all checkboxes were checked, of course):
{
'1': '1',
'2': '1',
'3': '1'
}
But what I got was a array, without the indexes I need to associate the result:
['1', '1', '1']
This seems like a bug to me. If I prepend the indexes with some string (like id-
), I get the object but... Is there a workaround so I don't need to change this to every form element and also every controller?
1) You can write middleware that will do it for all your controllers
2) You can stop using express.bodyParser
and parse data via middleware by yourself like this
app.use (function(req, res, next) {
var data='';
req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
req.on('end', function() {
req.body = data;
next();
});
});
in this way you will get data formed as you want