I am trying to upload an array of image URI's from application cache to firebase storage.
Once the images have been uploaded to firebase storage, the a callback to get the downloadURL for the file is called, and a document is created in firestore for the record. This record contains the first image. If multiple images have been taken, on the second photo, the firestore document is updated and new images are appended into image object (array).
Running this code has had 3 outcomes:
Any idea what I am doing incorrectly?
//Uploads Images
const uploadImages = async () => {
for (const image of images) {
const response = await fetch(image);
const blob = await response.blob();
const storageRef = firebase.storage().ref(REDACTED + ".jpg");
const task = storageRef.put(blob);
setLoading(true);
task.on('state_changed', taskSnapshot => {
console.log(`${taskSnapshot.bytesTransferred} transferred out of ${taskSnapshot.totalBytes}`);
});
await task.then(() => {
const dlURL = storageRef.getDownloadURL().then((url) => {
console.log("Download URL: ", url);
updateFirestore(url);
}).catch((error) => {
console.log("Error getting download url: ", error)
});
setLoading(false);
console.log("Image Successfully Uploaded");
}).catch((error) => {
console.error("Error Uploading Images: ", error);
setLoading(false);
});
}
}
//Updates table in Firestore
const updateFirestore = (dlURL) => {
console.log("PIC CODE: ", picCode);
if (picCode === 0) {
console.log("Creating Document...");
//Create
dbRef.add({
...
images: [
dlURL
]
}).then((result) => {
console.log(result);
setDocID(result.id);
//picCode++;
}).catch((error) => {
console.error(error)
});
} else {
console.log("Updating Document...", docID);
//Update
dbRef.doc(docID).update({
images: firebase.firestore.FieldValue.arrayUnion(dlURL)
}).then((result) => {
console.log(result);
//picCode++;
}).catch((error) => {
console.error(error)
});
}
picCode++;
};
Log Output:
Image: file:///var/mobile/Containers/Data/Application/483FC505-968F-4850-87E3-8CC6DD824A53/Library/Caches/ExponentExperienceData/%2540arcade336%252Fempireportal/Camera/A68C3715-DD20-41C3-9711-E1C80E7B31A3.jpg
0 transferred out of 3777563
262144 transferred out of 3777563
786432 transferred out of 3777563
1835008 transferred out of 3777563