NodeJS FileUpload - Cannot read property Undefined

Im using NodeJS, ExpressJS, mongoose, EJS.

upload form:

<form method='post' enctype='multipart/form-data'>
    <input type='text' name='photo[name]'/>
    <input type='file' name='photo[image]'/>
    <input type='submit' value='Upload'/>
</form>

model:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/photo_app');
var schema = new mongoose.Schema({
    name: String,
    path: String
});

module.exports = mongoose.model('Photo', schema);   

app.js:

var app = express();
...
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.bodyParser());
...

route:

var Photo = require('../models/Photo');
var path = require('path');
var fs = require('fs');
var join = path.join;

exports.submit = function (dir) {
    return function(req, res, next){
        var img = req.files.photo.image;
        var name = req.body.photo.name || img.name;
        var path = join(dir, img.name);
        fs.rename(img.path, path, function(err){
            if (err)
                return next(err);
            Photo.create({
                name: name,
                path: img.name
            }, function (err) {
                if (err)
                    return next(err);
                res.redirect('/');
            });
        });
    };
};

The problem when trying to upload:

500 TypeError: Cannot read property 'photo' of undefined

I had this same issue and what I did was pass a couple of options into the bodyParser object.

app.use(express.bodyParser({ keepExtensions: true, uploadDir: __dirname + '/public/photos' }));

After I put added the option everything seemed to work fine for me.