简体   繁体   English

如何使用 .map 方法渲染来自 Firebase 存储的图像?

[英]How do you render an image from Firebase Storage using .map method?

I'm attempting to render an image for each component using the.Map() method from Firebase Storage combined with Firestore.我正在尝试使用 Firebase 存储中的 .Map() 方法结合 Firestore 为每个组件渲染图像。

            {Object.values(getData).map((data, index) => {
          getDownloadURL(ref(storage, `events/${data.name + data.artist + data.date + data.description}` ))
          .then((url) => {
            setImageSet(url)
            console.log(imageSet)
          })
          
          return(

          <CssVarsProvider>
            <Card variant="outlined" sx={{ width: 300, marginRight: 5}}>
              <AspectRatio>
                <div>
                  <img src={imageSet}/> 
                </div>
              </AspectRatio>
              <Typography mt={2}>{data.name}</Typography>
              <Typography level="body2">{data.description}</Typography>
            </Card>
            </CssVarsProvider>
            
          )
        })}

When I create 1 test Array on Firestore, the image renders.当我在 Firestore 上创建 1 个测试数组时,图像呈现。 But if I create two, then it is like it is trying to render two images on 1 component.但是如果我创建两个,那么它就像是试图在 1 个组件上渲染两个图像。 How do I fix this?我该如何解决? Sorry if I am not clear enough, since this is my first time posting on Stackoverflow.对不起,如果我不够清楚,因为这是我第一次在 Stackoverflow 上发帖。

Cheers干杯

I have fixed the issue by assigning the URL value directly into firestore.我已通过将 URL 值直接分配给 firestore 来解决此问题。

  const uploadImage = async () => {
if (imageUpload == null) return
const imageRef = ref(storage, `events/${eventName + artistName + eventDate + eventDescription}`)
uploadBytes(imageRef, imageUpload).then(() => {
  console.log("image uploaded")
  getDownloadURL(imageRef)
    .then((url) => {
      setGetImage((prev) => [...prev, url])
      console.log(getImage)
    })
  
})

} }

const uploadData = async () => { const uploadData = async () => {

await setDoc(doc(db, "events", eventName), {
  name: eventName,
  artist: artistName,
  date: eventDate,
  description: eventDescription,
  uri: getImage,
})
console.log("db added")

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Firebase 函数在 Firebase 存储上上传一个 JSON 的新文件? - How do you upload a new file with a JSON on Firebase Storage using Firebase Functions? 如何在不重建的情况下渲染 Firebase 中的图像? - How to render image from Firebase without rebuilding? 如何从 Firebase 存储中获取图像文件名? - How to get image filename from Firebase Storage? 如何从 Flutter 中的 Firebase 存储中检索图像 URL? - How to retreive a image URL from Firebase Storage in Flutter? 如何从 firebase 存储下载图像,然后使用 JavaScript 重新上传但名称不同 - how to download an image from firebase storage then reupload it but with different name using JavaScript 如何等待firebase存储上传图片后再使用? - How to wait for firebase storage to upload image before using it? Android中如何使用Kotlin上传图片到Firebase存储 Q - How to upload an Image to Firebase Storage using Kotlin in Android Q 如何从 firebase 存储中获取用户个人资料图片 - How to fetch user profile image from firebase storage 如何从 Firebase 存储中获取图像的下载 url? - How to get download url for a Image from Firebase Storage? 从 firebase 下载图像存储在 swift - download image from firebase storage in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM