ExpressJS & Multer - req.body returns [Object: null prototype] {} when uploading file

Viewed 58

I have a route to upload files and I'm using multer as a middleware.

Express Server Code:

const multer = require("multer");

app.use(express.json());
app.use(express.urlencoded({extended : true}));

const multerUpload = multer();
const multerMiddleware = multerUpload.single('post_file');

app.post('/setupPictures', multerMiddleware, (req, res) => {
    console.log(req.body);
    res.send({"status" : "200"});
})

JQuery AJAX Code

var formData = new FormData();
var myFile = $(`#myFileInput`)[0].files[0];
formData.append('post_file', myFile)

$.ajax({
    url: `https://example.com/setupPictures`, 
    type: "POST",
    dataType: "json",
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    },
    processData: false,
    contentType: false,
    data: formData,
    success: function (result) {
        console.log(result);
    }
});

I'm uploading a .jpg file.

Upon sending the request, it responded with {"status" : "200"}. Also upon checking the Chrome's Network tab, the payload did indeed got sent. enter image description here

Problem

However, on the Server, it output the following:

[Object: null prototype] {}

Desired Output

Is there a solution ? I've been searching online and it seems that the desired output should be as such:

[Object: null prototype] { post_file: 'example.jpg' }

Extra

I tried adding req.files & req.file However they both returned undefined

0 Answers
Related