1

i'm a beginner with mongoDB and gridfs. I have a code which save in the database an incoming image. But I'd like to resize the image before saving it. It tried to use sharp, but I don't really understand how it works.

I tried the following code, but it's not working

const storage = new GridFsStorage({
  url: mongoURI,

  file: (req, file) => {
    return new Promise((resolve, reject) => {
      const filename = req.params.userID;
      const fileInfo = {
        filename: filename,
        bucketName: "ProfilePic", 
      };
      resolve(fileInfo);
    });
  }
});

const upload = multer({ storage });

router.post("/setProfilePic/:userID", upload.single("picture"), async(req, res) => {
//code not working
  await sharp(req.file.path)
    .resize(500)
    .jpeg({quality: 50})
    .toFile(
        path.resolve(req.file.destination,'resized',image)
    )
// end of non working code
  return res.status(200).json({state: "ok"})
});
AMS
  • 11
  • 1

0 Answers0