1
votes

I am having this error

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received an instance of Object

but I dont know how to solve it:

Here is my code base:

router.post('/upload/image', fileUpload.single('file'), function (req, res, next) {
  let streamUpload = (req) => {
    return new Promise((resolve, reject) => {
      let stream = cloudinary.uploader.upload_stream(
        (error, result) => {
          if (result) {
            resolve(result);
          } else {
            reject(error);
          }
        }
      );
      console.log("Images ", req.files);  
      streamifier.createReadStream(req.files.buffer).pipe(stream);
    }).
      catch(error => { throw error; });
  };
  
  async function upload(req) {
      let result = await streamUpload(req);
      console.log(result);
  }
  
  upload(req);
});

Thank you anyone who help me solve this problem

2

2 Answers

1
votes

As you're using fileUpload.single('file'), you should use req.file not req.files

Note req.file.buffer is only available with memorystorage

const storage = multer.memoryStorage();
cont upload = multer({ storage: storage });

https://www.npmjs.com/package/multer#memorystorage

console.log("Images ", req.file);  
streamifier.createReadStream(req.file.buffer).pipe(stream);

https://www.npmjs.com/package/multer

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})
0
votes

cloudinary.uploader.upload_stream expects 2 parameters, first should be string | buffer and second should be a callback object (error, result).

You are passing (error, result) as the only parameter, I believe that's what causing the error.

cloudinary.uploader.upload_stream('YOUR BUFFER OR STRING HERE', (error, result)=>{})