question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multer is not populating req.body and req.file

See original GitHub issue

I am using Multer to parse a multipart form in a keystone environment and and not able to access the req.body and req.file data inside my route controller

routes/index.js

var keystone = require('keystone'),
middleware = require('./middleware');

var bodyParser = require('body-parser');

//multi-part form parser for FTP api
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({storage: storage});

exports = module.exports = function(app) {
    app.use(bodyParser.json({limit: '10mb'}));
    app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
    app.post('/upload_api', upload.single('formFile'), routes.api.uploadFTP);
};

routes/api/uploadFTP.js

var keystone = require('keystone');
var ftpMod = require('ftp');
var fs = require('fs');

exports = module.exports = function(req, res) {
    console.log("req.body is ");
    console.log(req.body);
    console.log("req.file is ");
    console.log(req.file);
    res.send("console.log() outputted to screen");
}

public/test-upload.html

<html>
    <body>
        <form name="sampleForm" enctype="multipart/form-data" action="/upload_api" method="post">
            <p>Method</p>
            <input type="text" name="method"><br>
            <p>Options</p>
            <input type="text" name="options"><br>
            <p>File</p>
            <input type="file" name="formFile"><br><br>
            <input type="submit" value="Click to Send">
        </form>
    </body>
</html>

The response i receive from nodejs is

>req.body is 
{}
req.file is 
undefined

I am expecting req.body to contain {method: “sometext”} and req.file to be populated

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
nickanna42commented, Jun 21, 2017

specifically, I noticed req.file is undefined while using upload.single() but req.files is populated.

This is at odds with the expected behavior according to the documentation

6reactions
HarisHashimcommented, Oct 1, 2017

This is what I just discovered for my case.

In server.js I have

app.use(multer({dest:'./public/uploads/'}).any());

while my routes is using uploads.single. This is what caused req.file to be undefined but req.files to be populated.

After correcting the app.use() line in server.js to

app.use(multer({dest:'./public/uploads/'}).single('file'));

It begin to work as expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

req.file is not populated but, req.body is using multer
In the client, send the FormData as the body of the request: const formData = new FormData() // photo is a Blob, filename...
Read more >
Multer: Easily upload files with Node.js and Express
Running body-parser in Express ... The code in the image above means that the req.body object is empty, which is to be expected....
Read more >
req.file.path undefined multer - You.com | The AI Search ...
Req.file Is Undefined Multer When working with the Multer library in Node.js, you may encounter the “req.file is undefined” error. This can ...
Read more >
@neuralegion/multer - npm
Multer. Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files.
Read more >
Handling any POST data in Express - CodeX Team
Request body data will be placed to req.body . A few examples for decoding popular request data formats: application/x-www-form-urlencoded; application/json ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found