question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should req.files be an array?

See original GitHub issue

I’m really unsure of this myself, but I wanted to get the discussion started. Should req.files be an array?

It would certainly help #59 since there then would be only one array to check. I also think that the most common case is having one file upload which then would be really easy to find, req.files[0].

The downside would be pages that has multiple files with different fieldnames, it would be a bit more complicated to find a specific file.

To mitigate that we could expose a byFieldname(fieldname, multiple) function. fieldname would obviously be the filename and multiple would be a boolean on wether to allow multiple files or not. It would default to false.

If multiple is false and there is more than one file uploaded to that fieldname, I think that it should throw an error.

On top of this we could also expose a byMimetype(mimetype, multiple) function. That could help with sites that allows uploading any files, and then want to treat music, pictures or movies differently.

Although I feel that we are maybe providing a bit too much with the functions. If req.files is just a normal array, it’s very easy to do this with both built in functions, and already popular libraries.

examples

// Find all the new photos that should be added to the album
req.files.filter(function (file) {
  return (file.fieldname === 'photo')
})
// Check if the user has uploaded a new profile picture
_.find(req.files, function (file) {
  return (file.fieldname === 'profile-picture')
})
// Convert all audio files to ogg/vorbis
req.files.filter(function (file) {
  return is.is(file.mimetype, ['audio/*'])
})

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:59 (51 by maintainers)

github_iconTop GitHub Comments

8reactions
IvanMouchacommented, Nov 23, 2018

I’m having a problem with Typescript type checking regarding with multer.fields(). Typescript can’t check whether I used req.files.fieldname or `req.files[fieldname] it always shows an error.

When is used req.files.fieldname this is the error message from Typescript. " Property ‘Mp3’ does not exist on type ‘{ [fieldname: string]: File[]; } | File[]’. "

When is used req.files[fieldname] this is the error message from Typescript. " Element implicitly has an ‘any’ type because type ‘{ [fieldname: string]: File[]; } | File[]’ has no index signature. "

I had the same problem with TypeScript, but here is a “workaround”, it’s possible to cast this as a array.

req.files as Express.Multer.File[]

8reactions
mackignaciocommented, Jun 22, 2018

I’m having a problem with Typescript type checking regarding with multer.fields(). Typescript can’t check whether I used req.files.fieldname or `req.files[fieldname] it always shows an error.

When is used req.files.fieldname this is the error message from Typescript. " Property ‘Mp3’ does not exist on type ‘{ [fieldname: string]: File[]; } | File[]’. "

When is used req.files[fieldname] this is the error message from Typescript. " Element implicitly has an ‘any’ type because type ‘{ [fieldname: string]: File[]; } | File[]’ has no index signature. "

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accesing filenames in req.files created by Multer, doing stuff to ...
The result is not an array, although the multer docs say for multiple files, req.files will contain an array.
Read more >
Express multer middleware
An object with arrays of files will be stored in req.files . fields should be an array of objects with name and optionally...
Read more >
How to Upload Files in Node.js using Multer | by Amir Mustafa
Multiple files Upload - images key should be passed in postmanrouter.post("/multiple", upload.array("images"), (req, res) => { console.log(req.files); ...
Read more >
File upload | NestJS - A progressive Node.js framework
As you can see, it's required to specify an array of file validators that will be executed by the ParseFilePipe . We'll discuss...
Read more >
File Upload Using Multer in Node.js and Express.js
Combining Node.js with Express and the Multer library, we can smoothly implement the file ... The array of files will be saved in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found