I am developing a page and am using firebase storage as the file storage. I tried using the given codes in the firebase developer docs but I can't seem to make it work. What could be the problem?
document.getElementById("but").addEventListener('click', e => {
// Create a reference under which you want to list
const listRef = ref(storage, 'folder/');
// Find all the prefixes and items.
listAll(listRef)
.then((res) => {
res.prefixes.forEach((folderRef) => {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
});
res.items.forEach((itemRef) => {
// All the items under listRef.
console.log( getDownloadURL(ref(storage, `folder/${itemRef}`)))
})
})
});
I'm not sure what problem you have with the code, but one mistake is in :
res.items.forEach((itemRef) => {
// All the items under listRef.
console.log( getDownloadURL(ref(storage, `folder/${itemRef}`)))
})
The getDownloadURL function performs an asynchronous call to the server, so you'll need to either use await or then to capture that result:
res.items.forEach((itemRef) => {
getDownloadURL(ref(storage, `folder/${itemRef}`))
.then((downloadURL) => {
console.log(downloadURL);
});
})