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.

req.body will always empty when we use multer?

See original GitHub issue

Hi guys sorry for the noob question but I was trying to upload file from angular js using https://github.com/danialfarid/ng-file-upload. The problem is when I multer the req.body will always empty. Is this normal? if this is normal how could I send file with some data to name it like [data.id].[mimetype] Something like this

Thank you for your patience in reading my issue here is my Request Payload and code on server side

Request Payload

------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="file"; filename="AD ShoppingisGREATedit.jpg"
Content-Type: image/jpeg


------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="email"

xx@xxzc.com
------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="classId"

76
------WebKitFormBoundarykUG7UcxeBDOSFFb1--

At my Server side

router.post('/upload', function(req, res, next){
    console.log('uploading---')
    console.log(req)
    var storage = multer.diskStorage({
          destination: function (req, file, cb) {
            console.log(req.body)
            cb(null, ('hidden/images/slip/' +req.body.classId) )
          },
          filename: function (req, file, cb) {
            cb(null, file.fieldname + '-' + Date.now())
          }
        })

    //var upload = multer({ dest: ('hidden/images/slip/' + req.body.classId) }).single('file')
    var upload = multer({ storage:storage }).single('file')
    console.log(upload)
    upload(req,res,function(err) {
        if(err) {
            return handleError(err, res);
        }
        console.log("done upload---")
        res.json({"status":"completed"});
    });
})

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

106reactions
LinusUcommented, Mar 3, 2016

From the documentation:

Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to the server.

As you can see in your request payload, the file is sent before the classId field, and thus there is no way for multer to know about classId when it’s handling the file.

I wish I had an easy solution for you but the only thing I can recommend is to manually reorder the fields with javascript to make sure that fields are sent before files.

This has come up a number of times before, e.g. #146 where the workaround is outlined…

5reactions
fanghmcommented, Oct 28, 2016

It doesn’t work for me. but i found the real cause is, we should print req.body inside the upload() function, instead of outside of it (either before or after it).

Try adding below line console.log(req.body) near console.log("done upload---") in above code example, it should print all non-file type form fields .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js, multer and req.body empty - Stack Overflow
I am using multer to upload multiple files and single file in my nodejs ... next ) => { console.log( req.body ); //...
Read more >
@neuralegion/multer - npm
Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files. It is written on top of busboy ......
Read more >
Multer: Easily upload files with Node.js and Express
The code in the image above means that the req.body object is empty, which is to be expected. If you'll recall, body-parser ...
Read more >
[Solved]-Node.js, multer and req.body empty-node.js
The default express body-parser cannot work with multipart/form-data, hence we use multer to parse form-data which is accessible inside your upload function.
Read more >
Express body-parser middleware
This error will occur when the content of the request exceeds the configured parameterLimit for the urlencoded parser. The status property is set...
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