Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multer - req.file is undefined.

I am creating an app using Node, Express and Handlebars and multer for uploading images. Every time I submit the form, req.file is undefined. I've spent the entire day troubleshooting but can't figure out what I'm doing wrong.

Router File:

const express = require('express');
const router = express.Router();
const multer = require('multer');
const mongoose = require('mongoose');
const path = require('path');
const methodOverride = require('method-override');


//Set Storage Engine
const storage = multer.diskStorage({
    destination: './public/uploads/images',
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + 
    path.extname(file.originalname));
    }
});

const upload = multer({
    storage: storage
}).single('featuredImage');

//Change Featured Image - POST
router.post('/saveImage/:id', (req, res) => {
    console.log(req.file);

    //removed the rest of the code to keep it simple. req.file here is always undefined.

});

Form

<form action="/saveImage/{{pitch.id}}" method="POST" enctype="multipart/form-data">

    <div class="form-group">
        <label for="featuredImage">Featured Image</label>
         <input type="file" name="featuredImage" id="featuredImage">
     </div>

     <input type="submit" value="SAVE">
</form>

app.js these requires are in the app.js file.

const express = require('express');
const exphbs  = require('express-handlebars');
const path = require('path');
const passport = require('passport');
const mongoose = require('mongoose'); 
const bodyParser = require('body-parser');
const flash = require('connect-flash');
const session = require('express-session');
const methodOverride = require('method-override');
const nodemailer = require('nodemailer');

//Set StaticFolder
app.use(express.static(path.join(__dirname, 'public')));
like image 255
Uzair Avatar asked Feb 27 '18 09:02

Uzair


People also ask

What is DiskStorage in multer?

DiskStorage. The disk storage engine gives you full control on storing files to disk. const storage = multer. diskStorage({ destination: function (req, file, cb) { cb(null, '/tmp/my-uploads') }, filename: function (req, file, cb) { const uniqueSuffix = Date. now() + '-' + Math.

How do you catch errors in multer?

If you want to catch errors specifically from Multer, you can call the middleware function by yourself. Also, if you want to catch only the Multer errors, you can use the MulterError class that is attached to the multer object itself (e.g. err instanceof multer.

How do you use multer in frontend?

Multer will add the text inputs to req. body and add the files sent to the req. files array. To see this at work in the terminal, enter text and select multiple images on the frontend, then submit and check the logged results in your terminal.


2 Answers

You need to add upload.single('featuredImage') as middleware for the respective route as follows.

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

//Change Featured Image - POST
router.post('/saveImage/:id',upload.single('featuredImage'), (req, res) => {
    console.log(req.file);

   //removed the rest of the code to keep it simple. req.file here is always undefined.

});
like image 159
Bharathvaj Ganesan Avatar answered Sep 30 '22 07:09

Bharathvaj Ganesan


In my case it was issue with image size. I resolved it with defining multer limit as follows:

const upload = multer({ storage: storage, limits: { fieldSize: 10 * 1024 * 1024 } }); //10MB

Took me ages to figured out. Maybe this can help someone

like image 22
MartinM Avatar answered Sep 30 '22 06:09

MartinM