Now i know that there's a possibility that my question has a duplicate. But even so, I'd appreciate it if you directed me to the right answer if possible.
Am kinda new to new to javascript and my problem is that I am unable to loop through an array.
so here's my script
var reference = firebase.storage().ref();
var listRef = reference.child('images');
var img = document.getElementById('productImageOne');
var img2 = document.getElementById('productImageTwo');
var img3 = document.getElementById('productImageThree');
var links = [];
// Find all the prefixes and items.
listRef.listAll().then(function(res) {
res.prefixes.forEach(function(folderRef) {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
});
res.items.forEach(function(itemRef) {
// All the items under listRef.
var productRef1 = firebase.database().ref('Business/Products');
productRef1.on('value', function(snapshot) {
var products = snapshot.val();
products
const values = Object.values(products);
values.reverse();
for(const value of values){
//console.log(value["ShopName"])
if(value["ShopName"] === shop){
nombre = value["Name"];
console.log(itemRef.name.includes(value["Name"]));
if(itemRef.name.includes(nombre)){
itemRef.getDownloadURL().then(function(url) {
// `url` is the download URL for 'images/stars.jpg'
// This can be downloaded directly:
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
console.log(url);
links.push(url); //this is my array i want to iterate.
}).catch(function(error) {
// Handle any errors
console.log(error);
});
//this prints the array showing its contents normally
console.log(links);
console.log(links[1]); //this gives undefined
//for loop doesn't even run to begin with.
for(const link of links){
console.log(link);
console.log("this is looping");
}
}
}
}
});
});
}).catch(function(error) {
// Uh-oh, an error occurred!
console.log(error);
});
all am trying to do is get a list of image urls from firebase storage and store them in an array and then use a for loop to display each of them to the html page. Now, when i log console.log(links[1]) i get undefined, and the for loop does not run altogether.
Any with any ideas, please help out. regards.