【发布时间】:2021-01-17 01:24:26
【问题描述】:
我正在尝试检索存储在我的 Firebase 存储中的文件的下载 URL。我将一些数据与每个文件一起存储,因此是 firestore“文件”集合。 files 集合中的每个 doc 都是存储中的一个文件:
const [loading, setLoading] = useState(true);
const [itemData, setItemData] = useState(['']);
const dataRef = firebase.firestore().collection("files");
const fileRef = firebase.storage().ref();
// Load initial data
useEffect(() => {
return dataRef.get().then(querySnapshot => {
const list = [];
querySnapshot.forEach(doc => {
const { name, uri } = doc.data();
let imageRef = fileRef.child('/' + uri);
const imageUrl = imageRef
.getDownloadURL()
.then((url) => {
return url;
})
.catch((e) => console.log('Error getting image download URL: ', e));
console.log(imageUrl);
list.push({
id: doc.id,
name,
imageUrl: imageUrl
});
});
setItemData(list);
if (loading) {
setLoading(false);
}
})
.catch(function(error) {
console.log("Error getting documents: ", error);
alert("Error: no document found."); // It doesnt goes here if collection is empty
})
}, []);
问题是,console.log(imageUrl); 返回一个奇怪的对象:
但我的itemData 包含正确的下载 URL,但嵌套在某个对象的更深处?
不确定这里发生了什么。我只需要 url 就可以显示图片了。
【问题讨论】:
标签: javascript firebase react-native google-cloud-firestore