Skip to content

Fix for some of the issues discussed in issue #33 #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/incoming_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ IncomingForm.prototype.parse = function(req, cb) {
var fields = {}, files = {};
this
.on('field', function(name, value) {
fields[name] = value;
// check for arrays
if (name.indexOf("[]") === -1) { // not an array
fields[name] = value;
} else { // is an array
name = name.replace("[]","");
if (typeof fields[name] === "undefined") {
fields[name] = [];
}
fields[name].push(value);
}
})
.on('file', function(name, file) {
files[name] = file;
Expand Down
4 changes: 3 additions & 1 deletion test/simple/test-incoming-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ test(function parse() {
fn('field1', 'foo');
fn('field1', 'bar');
fn('field2', 'nice');
fn('fieldArray[]', 'first');
fn('fieldArray[]', 'second');
}

if (event == 'file') {
Expand All @@ -214,7 +216,7 @@ test(function parse() {
});

form.parse(REQ, gently.expect(function parseCbOk(err, fields, files) {
assert.deepEqual(fields, {field1: 'bar', field2: 'nice'});
assert.deepEqual(fields, {field1: 'bar', field2: 'nice', fieldArray: ['first','second']});
assert.deepEqual(files, {file1: '2', file2: '3'});
}));

Expand Down