title: Multer req.file.path "Cannot read property 'path' of undefined"
body: <p>I'm new to node.js and try to built a simple REST Api with file uploading capabilities. For file upload I'm using <code>multer</code> from npm and using POSTMAN for sending post request. But when I try to upload file with <code>multer</code> I got this error:</p>
  
  <pre><code>{
      "error": {
          "message": "Cannot read property 'path' of undefined"
      }
  }
  </code></pre>
  
  <p>Here is my code for the API</p>
  
  <p><strong><em>product.js</em></strong></p>
  
  <pre><code>import express from 'express';
  import mongoose from 'mongoose';
  import Product from '../models/product.model';
  import multer from 'multer';
  import multipart from 'connect-multiparty';
  
  const router = express.Router();
  
  const storage = multer.diskStorage({
      destination: (req, file, cb) =&gt; {
          cb(null, './uploads');
      },
      filename: (req, file, cb) =&gt; {
          cb(null, `${new Date().toISOString().replace(/:/g, '-')}${file.originalname}`);
      }
  });
  
  const fileFilter = (req, file, cb) =&gt; {
      // reject a file
      if(file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
          cb(null, false);
      } else {
          cb(new Error('Only .jpeg or .png files are accepted'), true);   
      }
  };
  
  const upload = multer({
      storage: storage,
      limits: {
          fileSize: 1024 * 1024 * 5
      },
      fileFilter: fileFilter
  });
  
  router.post('/', upload.single('productImage'), (req, res, next) =&gt; {
      const product = new Product({
          _id: new mongoose.Types.ObjectId(),
          name: req.body.name,
          price: req.body.price,
          productImage: req.file.path
      });
      product.save().then(result =&gt; {
          console.log(result);
          res.status(201).json({
              message: 'Created product successfully',
              createdProduct: {
                  name: result.name,
                  price: result.price,
                  _id: result._id,
                  request: {
                      type: 'GET',
                      url: `http://localhost:3000/products/${result._id}`
                  }
              }
          });
      }).catch(err =&gt; {
          console.log(err);
          res.status(500).json({
              error: err
          });
      });
  });
  </code></pre>
  
  <p>And here is the model</p>
  
  <p><strong><em>product.model.js</em></strong></p>
  
  <pre><code>import mongoose from 'mongoose';
  
  const productSchema = mongoose.Schema({
      _id: mongoose.Schema.Types.ObjectId,
      name: {type: String, required: true},
      price: {type: Number, required: true},
      productImage: { type: String, required: false }
  });
  
  module.exports = mongoose.model('Product', productSchema);
  </code></pre>
  
  <p>Even I use <code>connect-multipart</code> middleware to fix the issue but then I got other error:</p>
  
  <pre><code>{
      "error": {
          "message": "stream ended unexpectedly"
      }
  }
  </code></pre>
  
  <p>Here is the code when use <code>multipart</code></p>
  
  <pre><code>router.post('/', upload.single('productImage'), multipart(), (req, res, next) =&gt; {
      const product = new Product({
          _id: new mongoose.Types.ObjectId(),
          name: req.body.name,
          price: req.body.price,
          productImage: req.file.path
      });
      product.save().then(result =&gt; {
          console.log(result);
          res.status(201).json({
              message: 'Created product successfully',
              createdProduct: {
                  name: result.name,
                  price: result.price,
                  _id: result._id,
                  request: {
                      type: 'GET',
                      url: `http://localhost:3000/products/${result._id}`
                  }
              }
          });
      }).catch(err =&gt; {
          console.log(err);
          res.status(500).json({
              error: err
          });
      });
  });
  </code></pre>
  
  <p>I don't think it's a problem about <code>POSTMAN</code>.</p>
  
  <p>Now can anyone help me to fix this. It's really a big problem for me to go ahead without solving the issue....</p>
  
  <p>Thanks in advance.</p>