getDownloadurl在一系列字典中(重新渲染问题,foreach)
我有一系列字典,(例如[{},{},{},{}],每个字典都包含有关书籍的信息)
我想使用get downloadurl
从firebase存储中下载图像。
我当前的代码很像...
const [resObj, setresObj] = useState() // empty variable for update state
let result = [] //create empty array for copy & push new obj
useEffect(() => {
props.resObj.forEach((obj) => { // props.resObj: array of dictionary I explained before
const jpgName = 'bookDB/'+ obj.도서번호 + '.jpg';
const imgRef = ref(storage, jpgName)
getDownloadURL(imgRef)
.then((url) => {
result1.push({
...obj,
bookUrl: url
}) // copy & push dictionary
})
.catch((error) => {
if (error.code === 'storage/object-not-found') {
console.log('이미지 파일 없음')
result1.push({
...obj,
bookUrl: "https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg"
})
} else { console.log(error)}
})
})
setresObj(result1)
}, [])
此代码更新“ resobj”变量之后, 我将resobj映射为像...不幸的..没有显示的内容
return (
<div>
{resObj? resObj.map(item => {
<img
key = {}
className = '~~'
onClick = {}
src = {item.bookUrl}
/>
})}
</div>
)
没有显示..
。它似乎 foreach , useffect , usestate , getDownloadurl Promise 严重纠缠..
我尝试了1)devide下载图像代码作为函数,2)devide下载图像代码作为后退,3)逃脱从useeffect中下载图像代码,4)...(代码的额外差异)...
I have an array of dictionaries, (e.g. [{}, {}, {}, {}], each dictionary contains information about book)
I want to download image from firebase storage using getDownloadURL
.
My current code's like...
const [resObj, setresObj] = useState() // empty variable for update state
let result = [] //create empty array for copy & push new obj
useEffect(() => {
props.resObj.forEach((obj) => { // props.resObj: array of dictionary I explained before
const jpgName = 'bookDB/'+ obj.도서번호 + '.jpg';
const imgRef = ref(storage, jpgName)
getDownloadURL(imgRef)
.then((url) => {
result1.push({
...obj,
bookUrl: url
}) // copy & push dictionary
})
.catch((error) => {
if (error.code === 'storage/object-not-found') {
console.log('이미지 파일 없음')
result1.push({
...obj,
bookUrl: "https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg"
})
} else { console.log(error)}
})
})
setresObj(result1)
}, [])
after this code update 'resObj' variable,
I map resObj in component like...
return (
<div>
{resObj? resObj.map(item => {
<img
key = {}
className = '~~'
onClick = {}
src = {item.bookUrl}
/>
})}
</div>
)
unfortunately.. it doesn't show nothing..
It seems that forEach, useEffect, useState, getDownloadURL Promise seriously entangled..
I tried 1) devide download image code as function, 2) devide download image code as recoil, 3) escape download image code from useEffect, 4) ...(extra variances of code)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您对
setResobj
的调用发生在任何调用result1.push
发生之前,因此您始终设置一个空数组。通过设置断点并在调试器中运行或添加一些console.log
调用来验证这一点。修复程序是使用
Promise.all
等待已检索所有下载URL,然后调用setResobj
。这样的事情:The problem is that your call to
setresObj
happens before any of the calls toresult1.push
have happened, so you're always setting an empty array. It's easiest to verify this by setting breakpoints and running in the debugger, or by adding someconsole.log
calls.The fix is to use
Promise.all
to wait for all download URLs to have been retrieved and only then callsetresObj
. Something like this: