[英]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.