Skip to content
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

Force form.multiples to always return array #315

Closed
wants to merge 1 commit into from
Closed

Force form.multiples to always return array #315

wants to merge 1 commit into from

Conversation

gabeio
Copy link
Contributor

@gabeio gabeio commented Nov 30, 2014

When uploading one file to a formidable with form.multiples = true then the req.files.[name] would just be the file instead should be array of files as suggested by the documentation.

@gabeio
Copy link
Contributor Author

gabeio commented Nov 30, 2014

This would close #292.

@@ -91,7 +91,10 @@ IncomingForm.prototype.parse = function(req, cb) {
}
files[name].push(file);
} else {
files[name] = file;
if (!Array.isArray(files[name])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!Array.isArray(files[name]) is implied by !files[name]; this would just be files[name] = [file];. (Still, given the Array.isArray check above, it looks as if the intent was for single files not to be arrays.)

Copy link
Collaborator

@xarguments xarguments left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contains logical duplication that can be simplified:

It all can be simplified down to this:

if (this.multiples) {
  if (!files[name]) {
    files[name] = [];
  }
  files[name].push(file);
}

Note that it will do the same (will return always an array). But has no complicated/odd else blocks.

Anyway it should also be documented as well, and perhaps may need a version change (as its not backward compatible).

@tunnckoCore
Copy link
Member

You right requested change, but it was around 4 years? Isn't better to just make another PR, or actually just update this one directly.

@kornelski
Copy link
Contributor

Requirements for this feature:

  • 100% backwards compatible. Leave the existing field exactly as it is. This package has been stable for a very long time. Let's avoid splitting userbase by bumping semver-major. New feature can be added to a new field.

  • The user shouldn't need to sniff types to use it properly. Specifically, it can't be sometimes a file and sometimes an array. The new field that supports multiple files should always be an array with 1 or more items. This way it won't be possible to break applications that hardcode read of .name by sending two files under name.

@charmander
Copy link
Contributor

Specifically, it can't be sometimes a file and sometimes an array.

That’s how it already works.

@xarguments
Copy link
Collaborator

xarguments commented Jun 25, 2018

  1. Agree, we shouldn't break backward compatibility for it.
  2. From the other side, it's like (bad) inconsistency - the same code can return object and array,
    and dev should check it before handling it... And there are lots of open issues which are related to it, so devs surely want it (and we can close bunch of those "issues" if we provide it)

So, we can make it configurable, e.g. provide a setting which makes it always return array. Feature request?

@tunnckoCore
Copy link
Member

tunnckoCore commented Nov 28, 2019

Ooops, I just merged #380, should it be supported only behind the multiples option? There wasn't a check for this.multiples.

/cc @xarguments @kornelski @charmander @GrosSacASac

@tunnckoCore
Copy link
Member

Okay, my bad, that's for files and #380 is for fields, haha.

@GrosSacASac
Copy link
Contributor

I propose: Make the multiple always return arrays for both fields and files.

It does not need a new flag and is backwards compatible as application using multiples: true should already have an if else for arrays or objects.

I can make a new PR.

@GrosSacASac
Copy link
Contributor

Continue disscussion #292

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants