Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

255
Views
How to display images stored in mongoDB through GridFS

I'm trying to display images stored in mongoDB through GridFS. I don't quite understand it's implementation.

Here's the route to get the image:

router.post("/image", async (req, res) => {
    let { filename } = req.body
    const image = await gfs.files.findOne({ filename: filename });
    try{
        const readStream = gfs.createReadStream(image.filename);
        readStream.on("error", function(err){
            res.send("No image found with that title");
        });
        readStream.pipe(res);
    } catch (err) {
        res.status(500).json({error: err.message}); 
    }
});

It's a post method because I will not be displaying the filename on the url, I plan on displaying the image on a modal from the material ui. On a button click, I will be fetching three different photos.

Here's the request, which takes the filename of the photo:

const getVerificationPicture = filename => {
  return new Promise(async (resolve, reject) => {
    try {
      const result = await axios.post(
        "http://localhost:5000/api/verifications/post/image",
        filename
      );
      resolve(result);
    } catch (error) {
      reject(error);
    }
  });
};

I tried to save the result in variables and then use the variables to display them in an image tag, as follows:

const [images, setImages] = useState({})

const handleOpenModal = async (open, frontCardName, backCardName, faceName) => {
    setOpenModal(open);
    try {
      const frontID = await getVerificationPicture({ filename: frontCardName });
      const backID = await getVerificationPicture({ filename: backCardName });
      const faceID = await getVerificationPicture({ filename: faceName });
      setImages({
        ...images,
        frontImage: frontID.data,
        backImage: backID.data,
        faceImage: faceID.data,
      });
    } catch (error) {
      console.error(error);
    }
};

// in the modal component
export default function RequestsModal({ open, setOpen, images }) {
  return (
    <Dialog open={open} onClose={() => setOpen(false)}>
      ...
      <DialogContent>
        ...
        <img src={images.frontImage} />
        ...
      </DialogContent>
      ...
    </Dialog>
  );
}

However, the images were of type string, and when logged were a bunch of characters. When I try this in postman, I get the image back, but it doesn't display here. How could I display these on the img tag?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!