how to get images from mongo with axios and display them with react how to get images from mongo with axios and display them with react mongoose mongoose

how to get images from mongo with axios and display them with react


Try this, if the binary data is represented as a base64 string:

<img src={`data:image/jpeg;base64,${availableFile.data}`} />

Ref: How to display binary data as image in React?

Why are you using div to display an image though?


Ok so i figured it out, i'll share the solution. to make this work i needed to do 2 things:

  1. the first one is what @Viet said.

  2. and then i need to add {image.img.data.toString('base64')} to the get request:

    router.get('/images', async (req, res) => {      const image = await Image.findOne({})      res.json(image.img.data.toString('base64'))})