I am trying to submit a form which has a file input within it with a name of
profileimage
app.js
var multer = require('multer');
var upload = multer({ dest: './uploads' });
app.use(multer({dest: './uploads'}));
routes
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...');
...
Cannot read property 'profileimage' of undefined
req.files.profileimage
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.