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

153
Views
Reactjs problem rendering binary image retrieved from mongodb

Hello so I have images in a mongodb database and I'm trying to render them on the client side however It's not working. I convert the buffer unit8 data into base64 so I can render it it seemd to work and been stored in the images state but images are not accessible by associative array.

 useEffect( () => {
        const getAllDoctors = async () => {
            const result = await api.get('doctor/all')
            const myImages = []
            setDoctors(result.data)
            await result.data.forEach(async doctor => {
                myImages[doctor._id] = await base64_arraybuffer(doctor.photo.data.data)
            })
            setImages(myImages)
            setLoading(false)
        }
        getAllDoctors()

    }, [])

as for the render

return (
        <div>
            {
                images.map((image, key) => {
                    console.log(doctors)
                    return (
                        <div key={key}>
                            <img alt={'image'} src={`data:image/png; base64, ${image}`}/>
                            <div>{`Doctor + ${images}`}</div>
                        </div>
                    )
                })
            }
        </div>
    );

converter (not mine):

const base64_arraybuffer = async (data) => {
        const base64url = await new Promise((r) => {
            const reader = new FileReader()
            reader.onload = () => r(reader.result)
            reader.readAsDataURL(new Blob([data]))
        })

        return base64url.split(",", 2)[1]
    }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Two things

  • The way your array assignment is doesn't make sense. If _id is a ID string, then you are using a string to key an array, the data assigned at that key won't be included in loops. Use Array.prototype.push to add the item to the next available index.

  • Array.prototype.forEach and async / await don't play nice together due to the nature of callbacks. Try using a traditional loop.

useEffect(() => {
  const getAllDoctors = async () => {
    const result = await api.get("doctor/all");
    const myImages = [];
    setDoctors(result.data);
    
    for (const doctor of result.data) {
      const image = await base64_arraybuffer(doctor.photo.data.data);
      myImages.push(image)
    }
    
    setImages(myImages);
    setLoading(false);
  };
  getAllDoctors();
}, []);
about 4 years ago · Santiago Trujillo Report
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!