Search code examples
javascriptreactjsfetch

Res.buffer is not a function error while fetching data


I am trying to use fetch in my create-react-app to get images and import it into my sanity schema. I am trying to fetch the image and use res.buffer. But this just directly shows

TypeError res.buffer is not a function

The code I am using is given below:

import React from "react";

const Parser = () => {
  const fetch = require('node-fetch');
  const filePath =
    "https://upload.wikimedia.org/wikipedia/commons/1/15/White_Persian_Cat.jpg";
  fetch(filePath).then((res) => res.buffer())
 
  return (
<div></div>
)

};

export default Parser;

why could this error be occurring here?


Solution

  • I was able to solve this by using arrayBuffer() instead of just buffer().

    import React from "react";
    
    const Parser = () => {
      const fetch = require('node-fetch');
      const filePath =
        "https://upload.wikimedia.org/wikipedia/commons/1/15/White_Persian_Cat.jpg";
      fetch(filePath).then((res) => res.arrayBuffer())
     
      return (
    <div></div>
    )
    
    };
    
    export default Parser;