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 : req.body is null

See original GitHub issue

Hello,

I’m trying to upload a file through an express API. However when I try to fetch the body content, among the file, it gaves the following value : [Object: null prototype] {}

Here is the request that I’ve made with Postman : Capture d’écran_2021-08-04_17-47-07

As you can see, the file is the last element to be sent, it shouldn’t be a problem.

Here is my backend code :

app.post("/register/supplier",(req, res) => {

    const storage = multer.diskStorage({
        destination: (req,res,cb) => cb(null,__dirname + '/../files/kbis/'),
        filename: (req,file,cb) => cb(null, file.name)
    })
    
    const upload = multer({ storage }).single('kbis')
    
    upload(req,res,(err) => {
        console.log(req.body,req.files)
    })

    
    return res.status(200).json({ message: 'testing' })

});

I know that a few people met this req.body issue, and I tried to follow the proposed leads, but it didn’t resolve my issue. Any ideas ? Thank you for your help !

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ii02735commented, Aug 5, 2021

Oh you’re absolutely right, I didn’t pay attention !

Well, it works totally as expected, thank you a lot for your help 😄

0reactions
LinusUcommented, Aug 5, 2021

Since you are using .single(...) the file will live at req.file (no s)

const express = require('express');
const app = express();

const multer = require('multer');

const upload = multer().single('kbis')

app.post("/register/supplier", upload, (req, res) => {
    console.log(req.body, req.file)

    return res.status(200).json({ message: 'testing' })

});

app.listen(4000,() => console.log("listening"))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js, multer and req.body empty - Stack Overflow
Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to...
Read more >
req.body will always empty when we use multer? #322 - GitHub
The problem is when I multer the req.body will always empty. ... destination: function (req, file, cb) { console.log(req.body) cb(null, ...
Read more >
Multer — req.body null object when send file with other field ...
I was work in some project that built with NextJS as Front End and ExpressJS as Back End, when i create Rest API...
Read more >
Express multer middleware
Note that req.body might not have been fully populated yet. It depends on the order that the client transmits fields and files to...
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