Fix Multer error when @UploadedFiles() is used twice in a route #1620
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
All Submissions:
Closing issues
Closes #1619.
Potential Problems With The Approach
There are not that many files present in the test folder to use for testing uploading files. Right now the same file is included multiple times, but the order is always different, so this should not matter.
The
routeGenerator.ts
now also adds two extra attributes to each file upload field, namelymaxCount
andmultiple
. ThemaxCount
attribute is used by Multer to enforce only one file is uploaded to that field. However, HandleBars aims to be logic free, so a simple check (maxCount == 1
) cannot be done inhapi.hbs
to determine whether multiple files can be uploaded or not. Therefore, themultiple
attribute is also added, but by design of thejson
function in Handlebars, this extra variable is also included in the Express and Koaroutes.ts
. Luckily, it does not do anything, but it is not very elegant. If there is a better way to do this, I would love to hear it!Test plan
The unit tests that I added includes the case mentioned in the issue, but also two
@UploadedFiles()
statement to truly verify that these arrays are separated correctly.The fix simply removes the
upload.array()
lines from anyroutes.ts
files, because the same can be achieved by usingupload.fields()
.