ReactJS问题渲染从MongoDB检索的二进制图像

发布于 2025-01-23 12:35:44 字数 1605 浏览 4 评论 0原文

您好,所以我在MongoDB数据库中有图像,我正在尝试将其渲染到客户端,但是它不起作用。我将Buffer Unit8数据转换为base64,这样我就可以渲染它可以正常工作并存储在图像状态中,但是关联数组无法访问图像。

 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()

    }, [])

至于渲染

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>
    );

转换器(不是我的):

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]
    }

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]
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

匿名。 2025-01-30 12:35:44

两件事

  • 您的数组分配没有意义。如果_id是一个ID字符串,则使用字符串来键入数组,该键分配的数据不会包含在循环中。使用array.prototype.push将项目添加到下一个可用索引中。

  • array.prototype.foreach < / code>和async /等待由于回调的性质而不会一起玩。尝试使用传统循环。


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();
}, []);

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();
}, []);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文