Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda