diff --git a/lib/file-appender.js b/lib/file-appender.js index a760d8bb..8bc07ad6 100644 --- a/lib/file-appender.js +++ b/lib/file-appender.js @@ -43,6 +43,7 @@ FileAppender.prototype.removePlaceholder = function (placeholder) { case 'VALUE': break case 'ARRAY': arrayRemove(this.req.files, placeholder); break case 'OBJECT': + if (!this.req.files[placeholder.fieldname]) break if (this.req.files[placeholder.fieldname].length === 1) { delete this.req.files[placeholder.fieldname] } else { diff --git a/test/file-filter.js b/test/file-filter.js index 1d857f44..7783175f 100644 --- a/test/file-filter.js +++ b/test/file-filter.js @@ -53,4 +53,27 @@ describe('File Filter', function () { done() }) }) + + it('should not crash when fileFilter invokes the callback more than once', function (done) { + function rejectThenError (req, file, cb) { + setImmediate(function () { + cb(null, false) + cb(new Error('Fake error')) + }) + } + + var form = new FormData() + var upload = withFilter(rejectThenError) + var parser = upload.fields([ + { name: 'logo', maxCount: 1 }, + { name: 'banner', maxCount: 1 } + ]) + + form.append('logo', util.file('tiny0.dat')) + + util.submitForm(parser, form, function (err, req) { + assert.ok(err) + done() + }) + }) })