multer
multer copied to clipboard
Unclear error message "MulterError: Unexpected field"
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
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 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 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.'
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 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 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".
@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 yes. See https://github.com/expressjs/multer/issues/799#issuecomment-586526877
@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.
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 @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.
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()
.
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 Thanks man, Totally missed that
that the same error happens when the "nam
Yes sir, That was my error!! Thanks Man!
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.
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,
},
])
Same errror.
i have the same error, i am using uploads.fields