expressjs / multer

Node.js middleware for handling `multipart/form-data`.
MIT License
11.46k stars 1.04k forks source link

formData is not parser if not file is uploaded #972

Open Maxi-Di-Mito opened 3 years ago

Maxi-Di-Mito commented 3 years ago

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));
    };
};
trsmarc commented 1 year ago

I'm having this issue too. Just an upvote here

LinusU commented 1 year ago

This should work, and this test seems to cover it:

https://github.com/expressjs/multer/blob/4f4326a6687635411a69d70f954f48abb4bce58a/test/none.js#L33-L46

Are you still submitting the form as multipart/form-data?