0

I have a set of binary images stored in MongoDb: enter image description here

Binary('/9j/4AAQSkZJRgABAQEASABIAAD/4gIcSUNDX1BST0ZJTEUAAQEAAAIMbGNtcwIQAABtbnRyUkdCIFhZWiAH3AABABkAAwApADlh...', 0)

I get it with mongoose in Express server and send through API endpoint:

App.get("/getImages", function(req,res){
Picture.find({},function(err,foundImages){
    if(err){
        console.log(err);
        res.status(409).send(err);
    }else{
        res.status(200).send(foundImages);
    }
});

On the front end I make a call to that point from React:

useEffect(() => {

    fetch("/getImages", {
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
    })
    .then(response => { return response.json() })
    .then((result) => {
        console.log(result);
        setData(result);
    })

}, []);

But when I do console.log of one of the images, I get a long array of integers from 0 to 255 (853514 items).

data: (853514) [255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, 0, 72, 0, 0, 255, 226, 2, 28, 73, 67, 67, 95, 80, 82, 79, 70, 73, 76, 6...

I'm trying to display that image in img tag following this post. I've tried various options, but the latest one is this:

function Gallery(props) {
console.log(props.data.img);
let source = props.data.img.data;
console.log(source);
return (
    <figure className="gallery__item gallery__item--5">
        <img src={`data:image/jpg;base64,${source}`} alt="Gallery item 5" className="gallery__img"/>
        <figcaption className="gallery__item--descr">
            <span className="gallery__item--descr-title">{props.data.title}</span>
            <span className="gallery__item--descr-text">{props.data.description}</span>
        </figcaption>
    </figure>
);

}

export default Gallery;

It doesn't show the image. In the browser in the console it shows data:image/jpg;base64,[object Object]:1 GET data:image/jpg;base64,[object Object] net::ERR_INVALID_URL

It looks like it is trying to send a request somewhere.

Could you please explain what is going on here? I don't even know what to google.

3
  • In general, databases aren't great for storing images. It is better to use a cdn. You can store asset ids in your database. Then generate urls that point to your images on the cdn. It will be much faster for the user and much less load on your server. :) Commented May 5, 2020 at 1:22
  • Thank you for advice. I'll try to figure out how Firebase works. Commented May 6, 2020 at 0:02
  • @Jack_loc Hi, did you figure it out eventually? Commented Jun 23, 2021 at 13:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.