Multer TypeError: Cannot read property 'filename' of undefined

I have been battling with this error for days now and I thought it is best to ask for help so I can go to the next phase of my project.

I’ll list the files and their code below.

multer.js file

const multer = require("multer");
const path = require("path");
const { fileURLToPath } = require("url");

// 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 ) => {
      cb(null, path.join('../imgs/'));
    },
    filename: (req, file, cb) => {
        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
    },
})

repository.js file
Here, I imported my schema from gamemodel.

const Game = require("../models/gameModel");

exports.games = async () => {
    const games = await Game.find();
    return games;
}

exports.gameById = async id => {
    const game = await Game.findById(id);
    return game;
}

exports.createGame = async payload => {
    const newGame = await Game.createGame(payload);
    return newGame;
}

exports.removeGame = async id => {
    const game = await Game.findById(id);
    return game;
}

controller.js
I started getting confused here because I have tried different code options I saw on the internet

const gameRepository = require("../routes/repository")
const Game = require('../models/gameModel')

exports.createGame = async (req, res, next) => {
    
    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,
        })
    }
}

router.js

const express = require('express');
const router = express.Router();
const gameController = require("../controller/game.controller");
const multerInstance = require('./multer')


router.post("/game", multerInstance.single('image'),
gameController.createGame
)

router.get("/game", gameController.getGames);

router.get("/game/:id", gameController.getGameById);

router.delete("/game/:id", gameController.removeGame);


module.exports = router

Thank you very much

Hi @godswill199,

Is it possible for you to share your repo with this code as it is hard to understand the whole picture?

Thank you!
I will push and send it in a bit

Hi! Did you solve it? I’m having the same problem.