multer icon indicating copy to clipboard operation
multer copied to clipboard

Unclear error message "MulterError: Unexpected field"

Open Redsandro opened this issue 4 years ago • 18 comments

With multer, you can basically allow/expect one, multiple, none or any attachments.

When you send an attachment to an endpoint that doesn't accept one, you get the cryptic error message: "MulterError: Unexpected field".

Wondering if there is a critical problem and checking all my form fields for possible illegal characters, changing field names, etc, it turns out an attachment was sent but not expected.

I think the error message should be: Unexpected Attachment or Attachment Forbidden.


Reproduce

App

Exposed over serveo for testing purposes.

const express = require('express')
const bodyParser = require('body-parser')
const multer = require('multer')
const upload = multer({ dest: 'uploads/' })
const app = express()
const port = 3000

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/', (req, res) => res.send('Hello World!'))

app.post('/', upload.none(), (req, res) => {
	console.log('Incoming POST')
	console.log(JSON.stringify(req.body))
	res.send('Gotcha, thank you.')
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Mailgun

Sending a test request with this button

image

Error

MulterError: Unexpected field
    at wrappedFileFilter (/var/www/mailguntest/node_modules/multer/index.js:40:19)
    at Busboy.<anonymous> (/var/www/mailguntest/node_modules/multer/lib/make-middleware.js:114:7)
    at Busboy.emit (events.js:210:5)
    at Busboy.emit (/var/www/mailguntest/node_modules/busboy/lib/main.js:38:33)
    at PartStream.<anonymous> (/var/www/mailguntest/node_modules/busboy/lib/types/multipart.js:213:13)
    at PartStream.emit (events.js:210:5)
    at HeaderParser.<anonymous> (/var/www/mailguntest/node_modules/dicer/lib/Dicer.js:51:16)
    at HeaderParser.emit (events.js:210:5)
    at HeaderParser._finish (/var/www/mailguntest/node_modules/dicer/lib/HeaderParser.js:68:8)
    at SBMH.<anonymous> (/var/www/mailguntest/node_modules/dicer/lib/HeaderParser.js:40:12)

https://github.com/expressjs/multer/blob/805170c61530e1f1cafd818c9b63d16a9dd46c36/index.js#L38-L45

{
  encoding: "7bit",
  fieldname: "attachment-1",
  mimetype: "image/gif",
  originalname: "crabby.gif"
}

Alternatively the request just continue with the decoded form parameters, ignoring the unexpected attachments.

Redsandro avatar Oct 31 '19 10:10 Redsandro

@Redsandro I was able to reproduce the issue, When the server encounters unexpected file error is returned to multer-error.js

https://github.com/expressjs/multer/blob/59376904cf2317b3683368c8cbe3736356ffacd2/index.js#L40

In multer-error.js error mapping is defined, where 'LIMIT_UNEXPECTED_FILE' error is mapped to Unexpected field.

https://github.com/expressjs/multer/blob/59376904cf2317b3683368c8cbe3736356ffacd2/lib/multer-error.js#L10

For the request just continue with the decoded form parameters, ignoring the unexpected attachments, currently it is not supported. Let me know if this helps in clearing your doubts.

HarshithaKP avatar Nov 18 '19 09:11 HarshithaKP

@HarshithaKP thank you. I understand why this happens now.

The error message is still ambiguous. Could be helpful to others to clarify it.

'File not allowed in form-data with this method. Use ".any()" in stead.'

Redsandro avatar Nov 18 '19 17:11 Redsandro

I'm having this error message and cannot decode the thread.

What could I have done to receive the error "MulterError: Unexpected field"?

What is it expecting instead?

ogrotten avatar Feb 14 '20 19:02 ogrotten

@ogrotten this error message is ambiguous, and you could have probably figured it out on your own if the message was a bit less abstract.

I'm not sure what you are starting with, but every option has a set amount of accepted files.

Multer function Files accepted n
multer.none() 0 0
multer.any() 0+ n
multer.single() 1 1
multer.array() 1+ >=1
multer.fields() 1+ >=1

So you probably either used .none() and submitted a file, or .single() and submitted more than one file.

Without knowing what you're trying to do, the safest bet is probably to use .any().

Hope this helps.

Redsandro avatar Feb 15 '20 00:02 Redsandro

@Redsandro interesting . . . I used single() and only had one file, but I think the sent object may not have named the fields the way multer was expecting.

I appreciate the post tho... It basically means that if I come this way again, I'll likely use .any() "just in case".

ogrotten avatar Feb 22 '20 14:02 ogrotten

@Redsandro I am getting this error when posting a form without a file while using single(). Does that mean single() has to have at least one file?

aysiscore avatar May 19 '20 16:05 aysiscore

@aysiscore yes. See https://github.com/expressjs/multer/issues/799#issuecomment-586526877

Redsandro avatar May 19 '20 18:05 Redsandro

@Redsandro how can I configure it to accept 0 or a maximum of 1 file? The same route handles single file uploads as well as forms without any files... hence sometimes a form has 1 upload and sometimes 0.

aysiscore avatar May 20 '20 14:05 aysiscore

I think you can't. Either submit the form to a different handler based on whether or not a file was selected client-side, or accept any().

But this isn't my project. Maybe a maintainer or developer of this expressjs plugin can be of more help.

Redsandro avatar May 20 '20 16:05 Redsandro

@Redsandro @aysiscore To my understanding, that isn't the case.

When using single and sending a form without a file input field, or even an empty file input field, this error won't be triggered (assuming that if a file input field is present at all, there is only one and it named how single is expecting it to be).

See this bare bones example codesandbox. Click "Upload image" without selecting one. The error won't be triggered.

@aysiscore We'd need to see your upload route and how you're submitting the form from the client to better understand what the issue is. Feel free to open another issue.

jonchurch avatar May 20 '20 17:05 jonchurch

Hello all, I'm a newbie in node-express, my requirement is to upload multiple images at once. So on Postman, I have images[], and then I tried using upload.array('name'), didn't work. What works indeed is the upload.any().

glennposadas avatar Jun 19 '20 13:06 glennposadas

Hello everyone! I want to also mention that the same error happens when the "name" attribute in the file upload <input type="file" name="X" /> is not the same as in the upload.single("X")

ilyes42 avatar Jul 16 '20 17:07 ilyes42

@ilyes42 Thanks man, Totally missed that

AOthman97 avatar Jul 17 '20 05:07 AOthman97

that the same error happens when the "nam

Yes sir, That was my error!! Thanks Man!

emmanuelsio avatar Aug 03 '20 03:08 emmanuelsio

I had the same problem and then it is resolved when I changed the destination folder name. Check if your uploading file name and destination folder name are the same. If they are change the destination folder name.

enderdagdelen avatar Jan 14 '21 02:01 enderdagdelen

I am facing this problem. How can I achieve something like this?

uploads.fields([
    {
      name: "media", // This should be a required field
      maxCount: 1,
    },
    {
      name: "thumbnail",   // I want to make this field optional
      maxCount: 1, 
    },
    {
      name: "thumbnail_GIF", // // I want to make this field optional
      maxCount: 1,
    },
  ])

n1rjal avatar Jun 23 '21 16:06 n1rjal

Same errror.

NguyenKyThinh94 avatar Oct 28 '22 04:10 NguyenKyThinh94

i have the same error, i am using uploads.fields

hellowurld-create avatar Mar 11 '24 22:03 hellowurld-create