Node.js, multer and req.body empty

2017 Update

From Readme

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.

I resolved my issue by reversing the order of my form object properties in the front end:

    var newFormObj  = new FormData();
    newFormObj.append('internalUserID', internalUserID);
    newFormObj.append('listingImage', this.binaryImages[image]);

On the backend:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    console.log(req.body.internalUserID) // YAY, IT'S POPULATED
    cb(null, 'listing-pics/')
  },                    
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }                     
});                     

var upload = multer({ storage: storage });

Leave a Comment