Ollicca Mindstorm Ollicca Mindstorm - 1 year ago 159
Node.js Question

express multer Cannot read property 'profileimage' of undefined

I am trying to submit a form which has a file input within it with a name of

, i am using express 4, express generator and multer.

In my
I added multer like so:

var multer = require('multer');
var upload = multer({ dest: './uploads' });

The tutorial i am following actually sets up multer like so (after requiring it) but it gives me an error:

app.use(multer({dest: './uploads'}));

And in my
folder inside the respective file I have the following:

router.post('/register', function(req, res, next) {
// get form values
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;

// check for image field
if (req.files.profileimage)
console.log('Uploading file...');

However when I submit the form I get the following error:

Cannot read property 'profileimage' of undefined

It seems to not be able to understand
, but I don't know why?

Answer Source

The API for multer changed a bit some time ago, so some tutorials out there still use the old API.

For a single file, you would do something like:

var uploads = multer({dest: './uploads'});

// ...

router.post('/register', uploads.single('profileimg'), function(req, res, next) {

   // ...

   if (req.file) {
     console.log('Profile image uploaded');

See the multer documentation for more examples of other scenarios.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download