'Multer file uploder is not working in nodejs

I am trying to upload multi part form data using mutler but my req.body is always undefined and req.file.[fieldname] is also always undefined and multer is also not creating any folder for storage

here is my app.js

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();

app
  .use(logger('dev'))
  .use(express.json())
  .use(express.urlencoded({ extended: false }))
  .use(cors())
  .use(express.static(path.join(__dirname, './public')))
  .use('/api', require('./routes'))
module.exports = app;

my route file

const router = require('express').Router();
const companyController = require('../controllers/company.controller');
const { companyFileUpload } = require('../utils/companyFileUpload.utils');

router.post(
  '/register',
  companyFileUpload.fields([
    { name: 'profilePic', maxCount: 1 },
    { name: 'my-files', maxCount: 10 }
  ]),
  companyController.registerComapny
);
module.exports = router;

Multer middlware

const multer = require('multer');
const path = require('path');

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    if (file.fieldname === 'certificate') {
      // if uploading resume
      cb(null, 'uploads/user/company/certificates/');
    } else if (file.fieldname === 'profilePic') {
      // else uploading image
      cb(null, 'uploads/user/company/profilePics/');
    }
  },
  filename: (req, file, cb) => {
    cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
  }
});

const filefilter = (req, file, cb) => {
  if (file.fieldname === 'certificate') {
    // if uploading certificate
    if (
      file.mimetype === 'application/pdf' ||
      file.mimetype === 'application/msword' ||
      file.mimetype === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    ) {
      // check file type to be pdf, doc, or docx
      cb(null, true);
    } else {
      cb(null, false); // else fails
    }
  } else if (file.fieldname === 'profilePic') {
    // else uploading profile pic
    if (
      file.mimetype === 'image/png' ||
      file.mimetype === 'image/jpg' ||
      file.mimetype === 'image/jpeg'
    ) {
      // check file type to be png, jpeg, or jpg
      cb(null, true);
    } else {
      cb(null, false); // else fails
    }
  }
};

const companyFileUpload = multer({
  storage: storage,
  fileFilter: filefilter
});
module.exports = { companyFileUpload };

and my controller

const User = require('../models/User');
const Company = require('../models/Company');

module.exports.registerComapny = async (req, res) => {
  try {
    console.log(req.files['profilePic']);
  
    console.log(req.body); //undefined although I ma sending data
    res.status(200).json({
      status: 200,
      data: 'Your Account has been created. Please wait for the verification by Admin'
    });
  } catch (e) {
    console.log('ERROR:', e);
    return res.status(400).json({ status: 400, data: e.message });
  }
};

My server is running I am using postman to submit form data but my images is not getting saved also folder is not creating



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source