DEV Community

Discussion on: Image Upload to Cloudinary with Nodejs and Dotenv

Collapse
 
dimer191996 profile image
Dimer Bwimba
module.exports.createPost = async (req, res) => {
  let fileName;
  if (req.file) {
    console.log(req.file);
    try {
      if (
        req.file.mimetype != "image/jpg" &&
        req.file.mimetype != "image/png" &&
        req.file.mimetype != "image/jpeg"
      )
        throw new Error("invalid file");
      if (req.file.size > 500000) throw new Error("max size");
    } catch (error) {
      // const errors = uploadErrors(err);
      return res.status(401).send(error);
    }
    console.log(file);
    cloudinary.uploader
      .upload(req.file)
      .then((result) => {
        response.status(200).send({
          message: "success",
          result,
        });
      })
      .catch((error) => {
        response.status(500).send({
          message: "failure",
          error,
        });
      });
Enter fullscreen mode Exit fullscreen mode

this is my image

{
  fieldname: 'file',
  originalname: '0a628080d7386a847d18308ace6d15a6.jpg',  encoding: '7bit',
  mimetype: 'image/jpeg',
  buffer: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 06 04 05 06 05 04 06 06 05 06 07 07 06 08 0a 10 0a 0a 09 09 0a 14 0e 0f 
0c ... 14239 more bytes>,
  size: 14289
}
Enter fullscreen mode Exit fullscreen mode

error:

(node:2016) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Object

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Hey Dimer, you are sending in an object containing the file details instead of just sending in the path to the image in you local machine.

Check out this dev.to/ebereplenty/cloudinary-and-....

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

The path is supposed to read: "c:/nam/folder/.../file_name.extension" and cloudinary will take it from there