0

I want to upload an image to mongodb using multer. Here is my code :

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./uploads");
  },
  filename: function (req, file, cb) {
    cb(
      null,
      file.fieldname + "-" + Date.now() + path.extname(file.originalname)
    );
  },
});

const upload = multer({
  storage: storage,
});

Inside mongoClient:

client.connect((err) => {
const adminTasks = client
    .db(`${process.env.DB_NAME}`)
    .collection("adminTasks");

app.post("/uploadImage", upload.single("myImage"), (req, res) => {
    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, "base64"),
    };

    // inserting image to db
    userTasks.insertOne(final_image, (err, result) => {
      console.log(result);
      if (err) {
        console.log(err);
      }
      console.log("saved to db");
      res.contentType(final_image.contentType);
      res.send(final_image.image);
    });
  });
});

and the error is:

TypeError: Cannot read property 'path' of undefined

I have read many problems regarding that the problem occurs due to enctype="multipart/form-data". But I have used this:

<form action="http://localhost:5000/uploadImage" method="POST">
          <input type="file" enctype="multipart/form-data" name="myImage" />
          <input type="submit" value="upload Image" />
        </form>

How to fix this problem????

Fuad9
  • 353
  • 1
  • 3
  • 19
  • Does this answer your question? [How to get direct URL to multipart file uploaded via Node.js](https://stackoverflow.com/questions/59054367/how-to-get-direct-url-to-multipart-file-uploaded-via-node-js) – turivishal Oct 05 '20 at 06:30
  • no , I can not understand the suggested solution. – Fuad9 Oct 05 '20 at 10:50

2 Answers2

0

With upload.single(), the filename is in req.file. That's a string that represents the filename. There is no property req.file.path. So, you should be using req.file to get the filename.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • using req.file, the new error is----> TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined – Fuad9 Oct 05 '20 at 05:32
  • @Fuad9 - What does `console.log(typeof req.file)` show? – jfriend00 Oct 05 '20 at 05:48
  • the error is showing for --> const img = fs.readFileSync(req.file); at Object.openSync (fs.js:450:10) at Object.readFileSync (fs.js:360:35) and the typeof req.file is showing undefined – Fuad9 Oct 05 '20 at 05:54
  • @Fuad9 - OK, if `typeof req.file` shows `undefined, then you have an upstream problem with multer. Perhaps your `const storage = ...` part is wrong. I don't know multer enough to help you there. – jfriend00 Oct 07 '20 at 03:55
0

file.fieldname may be undefined. That is why the complete path becomes undefined.

Instead, try looking at req.file for the name. Here is what worked for me:

filename: (req, file, cb) => {
    cb(null, file.originalname)
}

The documentation incorrectly states in a general manner that the storage configuration should contain

filename: (req, file, cb) => {
    cb(null, file.fieldname)
}
Tae Soo Kim
  • 1,019
  • 8
  • 14