1

I've saved my image in Mongodb like that :

 "img": {
    "data": "<Binary Data>",
    "contentType": "image/png"
}

I try to display it in ReactJS using this code :

..{products.map(product => {
   <img src= {product.img} />

But nothing is displayed. Any idea please to fix it.

1 Answer 1

2

You are trying to display your image in src attribute with binary data when it expect an url.

If it's binary data you need to diplay it like this and tell to your component that you are passing binary data

<img src={`data:image/png;base64,${product.img.data}`} />
Sign up to request clarification or add additional context in comments.

3 Comments

I get this error : ` Syntax error: JSX value should be either an expression or a quoted JSX text (93:60)`
Try this new version. Also be sure to check how your binary data is formatted. Maybe it already have base64, or data:image/png;base64, so you need to adapt it. If you can give me an example of your binary data i can give you exactly what it's supposed to be
It's okey, So, i have edited the new code , the image cannot be displayed but i get new error : data:image/png;base64,[object Object]:1 GET data:image/png;base64,[object Object] 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.