expressjs / expressjs/multer

formData is not parser if not file is uploaded

Open
#972 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Hi im having this problem with my nextjs app.
I have an api route thas use the multer middleware, for that to work i have to tell next to disable the bodyparser for that route.
In the fronted the from has a field for a file and other text fields.
If i upload a file then i get all the data, the file and the text fields in the req.body.
But if i dont upload a file then the text fields are not populated.

` uploadRoute('imageFile', async (req, res) => {`
` console.info('REQ', req);`

and that uploadRoute middleware im using is here:
```
import multer from 'multer';
const uploader = multer();

export default (fieldName, handler) => {
// manejar cuando no se usa fieldName
if (typeof fieldName !== 'string') {
handler = fieldName;
fieldName = 'files';
}
const uploadStrategy = uploader.array(fieldName);
return (req, res) => {
uploadStrategy(req, res, () => handler(req, res));
};
};

```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the shown uploadRoute middleware with a multipart form containing text fields, first with a file and then without one. Compare req.body in both cases and trace multer's array middleware behavior; done means text fields are populated consistently when no file is uploaded.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
22/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.