Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "path" argument must be of type string or an instance of Buffer or URL cloudinary and nodejs

I am trying to create a method that client can upload image from front end to back end server and then store the images to cloudinary but I end up getting this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of Buffer or URL. Received undefined

Here is my backend code base:

const express = require("express");
const router = express.Router();
const { catchErrors } = require("../errors/errorHandlers");
const { body } = require("express-validator");
const multer = require('multer');
const cloudinary = require('cloudinary').v2;
const streamifier = require('streamifier');
const storage = multer.memoryStorage();
const fileUpload = multer({ storage: storage });
const fs = require('fs'); 

cloudinary.config({
  cloud_name: "xxx",
  api_key: "xxxx",
  api_secret: "xxxx",
});

this is what I got from console

Images  {
  file: {
    name: 'me.jpg',
    data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 f0 00 f0 00 00 ff e1 03 78 45 78 69 66 00 00 4d 4d 00 2a 00 00 00 08 00 09 01 0f 00 02 00 00 00 06 00 00 ... 442191 more bytes>,
    size: 442241,
    encoding: '7bit',
    tempFilePath: '',
    truncated: false,
    mimetype: 'image/jpeg',
    md5: 'a01832d1a390b2bcd7e2b6103d68eaa4',
    mv: [Function: mv]
  }
}

{ message: 'Request Timeout', http_code: 499, name: 'TimeoutError' }

How can I fix this problem?

like image 334
Nathan Nguyen Avatar asked Sep 05 '25 03:09

Nathan Nguyen


1 Answers

I had the same error. here how you solve it:

npm install datauri

const DatauriParser=require("datauri/parser");
const parser = new DatauriParser();

since you parse the form with multer and store it in memory storage:

  console.log("req.file object",req.file)
  const extName = path.extname(req.file.originalname).toString();
  const file64 = parser.format(extName, req.file.buffer);

file64 is a DataUri object. we cannot pass it directly

const result = await Cloudinary.upload(file64.content!);
console.log("result of Cloudinary upload",result")

this result object has "secure_url" property this is the secure https link to the file.

like image 90
Yilmaz Avatar answered Sep 07 '25 17:09

Yilmaz