We are working on a simple e-commerce app where we need to upload multiple product images. Using Vuejs and Vue-Croppa, we need to upload the images to firebase storage, retrieve the download URLs, then include those URLs in an array when we add that product to our database.
<croppa v-for="(c, i) in croppas"
:key="i"
v-model="croppas[i]"
:width="150"
:height="150"
@new-image="croppas.push({})"
v-show="i === croppas.length - 1 || c.imageSet"
></croppa>
And our method:
addProduct() {
this.showSuccess = true
let {details} = this
this.croppas.forEach(file => {
file.generateBlob(
blob => {
let rand = (Math.random().toString(36).substring(2, 16) + Math.random().toString(36).substring(2, 16)).toUpperCase()
let imagesRef = storageRef.child('productimages/' + rand)
let downloadURL
imagesRef.put(blob).then((snap) => {
imagesRef.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL)
})
})
},
'image/jpeg',
0.8
)
})
let shop = this.userStore.id
let {details} = this
console.log(shop)
let createdAt = new Date().toString()
let createdBy = this.currentUser.uid
console.log(createdBy)
let productImageUrl = downloadURL
fb.productsCollection.add({ details, createdAt, createdBy, shop})
.then( doc => {
fb.productsCollection.doc(doc.id).update({
id: doc.id,
})
})
.then( doc => {
fb.productsCollection.doc(doc.id).update({
productImages: productImageUrl
})
})
setTimeout(() => {
this.showSuccess = false
}, 4000)
},
Right now, we are getting a console error:
Firebase Storage: Invalid argument in 'put' at index 0: Expected Blob or File.
Also, the productImageUrl = downloadURL
would only work for one file, where there will be potentially more. How can me make this work with an array?
For your second issue.....
Set up a promise for each image you want to upload.
So in your loop call a method "similar" to the below (I've just grabbed this out of one of my projects):