Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I am using express-validator for validating fields in my form.
Consider my form has a field name and a file upload input images -
Below is my validator middleware array -

const validateAdd = [
    (req, res, next) => {
        const form = formidable({
            multiples: true,
            uploadDir: uploadDir,
            keepExtensions: true,
            filter: imageMimeFilter
        });
        form.parse(req, (err, fields, files) => {
            req.files = files;
            req.body = fields;
        });
        next();
    },
    body('name').trim().notEmpty().withMessage('Please fill this field'),
];

The issue is, form.parse calls my callback after some time (asynchronous, after parsing the file data), so my next middlewares body('name')... don't find values in the req.body.

In case user modifies the form type from form-data to x-www-form-urlencoded then the callback of form.parse is not called.
The examples given, includes the next() inside the form.parse callback function, which will also never be called if the form was not of type form-data and the server just hangs.

Sorry if I am asking a simple/stupid question, I am just starting to learn MERN.
Thanks in advance.

You must be logged in to vote

You should check the err variable,
next() should be inside the form.parse callback,
wrap form.parse it in a promise and await.
assign fields to the body

Replies: 1 comment · 7 replies

Comment options

You should check the err variable,
next() should be inside the form.parse callback,
wrap form.parse it in a promise and await.
assign fields to the body

You must be logged in to vote
7 replies
@lexNwimue
Comment options

Having a similar issue too.

const form = new formidable.IncomingForm();
    const uploadFolder = path.join(__dirname, "/uploads/");
    form.options.multiple = false;
    form.options.maxFileSize = 50 * 1024 * 1024; // 5MB
    form.options.uploadDir = uploadFolder; // Set the upload dir to our custom dir

    console.log(form, "...Form"); // This runs. 

    form.parse(req, async (err, fields, file) => {
      if (err) {
        console.log("Error parsing the file: ", err); // Doesn't run 
        return res.json({ err: "Some error occurred" + err.message });
      }

      console.log(fields, file); // Doesn't run too. 
@GrosSacASac
Comment options

maybe you have another middle ware that consumes the body.

Try to run the debugger in step by step mode to narrow the problem down

@lexNwimue
Comment options

maybe you have another middle ware that consumes the body.

Try to run the debugger in step by step mode to narrow the problem down

I fixed it, Bro. I wasn't even sending the file in the FormData on the frontend in the first place. :D

@yeasinjabed2
Comment options

Having the same problem
In my case when I false bodyPraser its work . but when I set some sizeLimiting it not work . I'm using next js .

export const config = {
  api: {
    bodyParser: {
      sizeLimit: '20mb'
    },
  },
};

.................
export default async (req, res) => {
console.log('run outside if set bodyparse false or  limit'); 

const forms = new formidable.IncomingForm();

form.parse(req, async (err, fields, file) => {
// doesn't run anything if I set size limit
}
.................
@GrosSacASac
Comment options

sizeLimit does not exist in Formidable ...

Answer selected by GrosSacASac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.