Multer is not populating req.body and req.file
See original GitHub issueI 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:
- Created 6 years ago
- Comments:26 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
specifically, I noticed
req.file
is undefined while usingupload.single()
butreq.files
is populated.This is at odds with the expected behavior according to the documentation
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.