multer: 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.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 6
- Comments: 18 (1 by maintainers)
Commits related to this issue
- Clarify ambiguous error message; closes #799 Two ambiguities when not expecting an error: What and where. Both addressed. — committed to Redsandro/multer by Redsandro 5 years ago
@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.none()
multer.any()
multer.single()
multer.array()
multer.fields()
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.
@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.’
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 usingupload.array('name')
, didn’t work. What works indeed is theupload.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 theupload.single("X")
@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 howsingle
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.
@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.