title: How to solve "can not read property path of undefined" in node.js(Multer)
body: <p>I want to upload an image to mongodb using multer. Here is my code :</p>
  <pre><code>const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, &quot;./uploads&quot;);
    },
    filename: function (req, file, cb) {
      cb(
        null,
        file.fieldname + &quot;-&quot; + Date.now() + path.extname(file.originalname)
      );
    },
  });
  
  const upload = multer({
    storage: storage,
  });
  </code></pre>
  <p>Inside mongoClient:</p>
  <pre><code>client.connect((err) =&gt; {
  const adminTasks = client
      .db(`${process.env.DB_NAME}`)
      .collection(&quot;adminTasks&quot;);
  
  app.post(&quot;/uploadImage&quot;, upload.single(&quot;myImage&quot;), (req, res) =&gt; {
      const img = fs.readFileSync(req.file.path);
      const encode_image = img.toString(base64);
  
      // defining json obj for image
      const final_image = {
        contentType: req.file.mimetype,
        path: req.file.path,
        image: new Buffer(encode_image, &quot;base64&quot;),
      };
  
      // inserting image to db
      userTasks.insertOne(final_image, (err, result) =&gt; {
        console.log(result);
        if (err) {
          console.log(err);
        }
        console.log(&quot;saved to db&quot;);
        res.contentType(final_image.contentType);
        res.send(final_image.image);
      });
    });
  });
  </code></pre>
  <p>and the error is:</p>
  <pre><code>TypeError: Cannot read property 'path' of undefined
  </code></pre>
  <p>I have read many problems regarding that the problem occurs due to enctype=&quot;multipart/form-data&quot;. But I have used this:</p>
  <pre><code>&lt;form action=&quot;http://localhost:5000/uploadImage&quot; method=&quot;POST&quot;&gt;
            &lt;input type=&quot;file&quot; enctype=&quot;multipart/form-data&quot; name=&quot;myImage&quot; /&gt;
            &lt;input type=&quot;submit&quot; value=&quot;upload Image&quot; /&gt;
          &lt;/form&gt;
  </code></pre>
  <p>How to fix this problem????</p>