title: Multer TypeError: Cannot read property ‘filename’ of undefined
body: <p>I have been battling with this error for days now and I thought it is best to ask for help.</p>
  <p>I’ll list the files and their code below.</p>
  <p>multer.js file</p>
  <pre><code>const multer = require(&quot;multer&quot;);
  const path = require(&quot;path&quot;);
  const { fileURLToPath } = require(&quot;url&quot;);
  
  // checking for file type
  const MIME_TYPES = {
      'imgs/jpg': 'jpg',
      'imgs/jpeg': 'jpeg',
      'imgs/png': 'png'
  }
  
  // Image Upload
  const storage = multer.diskStorage({
      destination: (req, file, cb ) =&gt; {
        cb(null, path.join('../imgs/'));
      },
      filename: (req, file, cb) =&gt; {
          const name = file.originalname.split('').join(__);
          const extension = MIME_TYPES[file.mimetype];
          cb(null, name + Date.now() + '.' + extension);
      }
  });
  
  module.exports = multer({
      storage: storage,
      limits: {
          fileSize: 1024 * 1024 * 6
      },
  })
  </code></pre>
  <p>repository.js file
  Here, I imported my schema from gameModel.</p>
  <pre><code>const Game = require(&quot;../models/gameModel&quot;);
  
  exports.games = async () =&gt; {
      const games = await Game.find();
      return games;
  }
  
  exports.gameById = async id =&gt; {
      const game = await Game.findById(id);
      return game;
  }
  
  exports.createGame = async payload =&gt; {
      const newGame = await Game.createGame(payload);
      return newGame;
  }
  
  exports.removeGame = async id =&gt; {
      const game = await Game.findById(id);
      return game;
  }
  </code></pre>
  <p>controller.js
  I started getting confused here because I have tried different code options I saw on the internet</p>
  <pre><code>const gameRepository = require(&quot;../routes/repository&quot;)
  const Game = require('../models/gameModel')
  
  exports.createGame = async (req, res, next) =&gt; {
      
      try {
          // req.body.game = JSON.parse(req.body.game)
          const hostname = req.hostname;
       const url = req.protocol + '://' + hostname + req.file.filename;
  
          const payload = new Game({
              title: req.body.name,
              price: req.body.price,
              category: req.body.category,
              gameIsNew: req.body.gameIsNew,
              topPrice: req.body.topPrice,
              isVerOrient: req.body.IsVerOrient,
              description: req.body.description,
              image: url + 'imgs/' + req.file.filename       //  The error is from here
          });          
  
          let eachGame = await gameRepository.createGame({
              ...payload
          });
  
          res.status(200).json({
              status: true,
              data: eachGame,
          })
  
      } catch (err) {
          console.log(err)
          res.status(500).json({
              error: err,
              status: false,
          })
      }
  }
  
  </code></pre>
  <p>gameRouter.js</p>
  <pre><code>const express = require('express');
  const router = express.Router();
  const gameController = require(&quot;../controller/game.controller&quot;);
  const multerInstance = require('./multer')
  
  
  router.post(&quot;/game&quot;, multerInstance.single('image'),
  gameController.createGame
  )
  
  router.get(&quot;/game&quot;, gameController.getGames);
  
  router.get(&quot;/game/:id&quot;, gameController.getGameById);
  
  router.delete(&quot;/game/:id&quot;, gameController.removeGame);
  
  
  module.exports = router
  </code></pre>
  <p>Thank you for your help in advance.</p>