I am trying to upload a IMG files from my computer to my Firebase Realtime Database I have the image location stored inside of a json array and it includes the other details of the data I want to upload im wondering how I can upload the img to firebase and get the download url . I currently have something working only problem is that the images upload but only 1 or 2 objects will show up in real time database other than the expected number of 9
function makeCustomersHappy(){
var count = 0;
fetch("./upcominTasks.json")
.then(response => {
return response.json();
})
.then(jsondata =>{
jsondata.forEach(position => {
alert(count)
count+=1;
var imageName = JSON.stringify(position.name).replaceAll('"','')//neccesary for storing data
var urlLink = JSON.stringify(position.url).replaceAll('"','')
var itemName = JSON.stringify(position.name).replaceAll('"','')
var store = JSON.stringify(position.store).replaceAll('"','')
//get item name
document.getElementById('sometext').textContent = imageName;
document.getElementById('upIMG').src = `./dropImages/${imageName}.jpg` //change image
//names the image for storage ref
const storageRef = sRef(storage, `upcomingTasks/${imageName}.jpg`);
var imgAddress = fetch(`http://127.0.0.1:5500/puppetBots/dropImages/${imageName}.jpg`)
.then(function(response) {
return response.blob()
}).then(function(img_blob) {
//location.reload()
//uploads the image to firebase
uploadBytes(storageRef,img_blob ).then((snapshot) => {
//the image that we just uploaded we get the dowload url....
getDownloadURL(sRef(storage, `upcomingTasks/${imageName}.jpg`))
.then((img_url) => {
writeUserData(count,itemName,urlLink,img_url,store)
});
});
});
});
});
}
async function writeUserData(id, name, url, imgLink,store) {
const db = getDatabase();
//id is basically the loop we are currently at
set(gRef(db, 'UpcomingDropLinks/' +id), {
ImageUrl: imgLink,
ItemName: name,
ItemUrl : url,
Store:store
});
}
makeCustomersHappy()