Home > Blockchain >  TypeError: Expected the input argument to be of type Uint8Array or Buffer or ArrayBuffer, got object
TypeError: Expected the input argument to be of type Uint8Array or Buffer or ArrayBuffer, got object

Time:09-17

So I have made a Meme Gallery app. Here is the link:https://meme-gallery-web-app.netlify.app/

You can upload an image via link. First of all I was trying to check if the submitted URL had the jpg/png extension, so I can work with it(whether to upload it or not). That's why I tried to implement Image-type package. But it gives me the mentioned error. Here is the code where I implemented.

const https = require("https");
const imageType = require("image-type");

const imageModel = require("../models/models.js");

var date = new Date();
var currentDate = date.toLocaleDateString();

const submitLink = async (req, res) => {
  const { url } = req.body;

  https.get(url, (response) => {
    response.on("readable", () => {
      const chunk = response.read(imageType.minimumBytes);
      response.destroy();

      console.log(imageType(chunk));
    });
  });
};

After submitting the link I got the following error:

TypeError: Expected the input argument to be of type Uint8Array or Buffer or ArrayBuffer, got object

So I checked and found out that the variable chunk is an object rather than an Uint8Array or Buffer. Why is that? And how to workaround it?

CodePudding user response:

I think you should read the chunks, concat them and pass the resulting buffer to the imageType library:

https.get(url, response => {
    const chunks = [];
    response.on("data", (chunk) => {
        chunks.push(chunk);
    })
    response.on("end", () => {
        const resultBuffer = Buffer.concat(chunks);
        console.log("image type is", imageType(resultBuffer ));
    })
});
  • Related