我正试图像这样使用Firebase Storage将多个图像URL读取到一个数组中,但在读取testArray中的特定索引位置时遇到问题:
var testArray = []
listAll(ref).then((res) => {
res.items.forEach((itemRef) => {
getDownloadURL(itemRef).then((url) => {
testArray.push(url)
})
});
}).catch((error) => {
// Uh-oh, an error occurred!
});
console.log(testArray[0])
将Firebase Storage URL存储在稍后代码中使用的阵列中的正确方法是什么?
感谢ChrisG的评论,我现在可以读取索引位置的数组了。解决方案如下:
const [testArray, setTestArray] = useState([]);
useEffect(() => {
listAll(ref)
.then(async (res) => {
const {items} = res;
const urls = await Promise.all(
items.map((item) => getDownloadURL(item))
);
setTestArray(urls);
})
.catch((error) => {
// Uh-oh, an error occurred!
});
}, [])
console.log(testArray[0])