In the following code. I'm running a loop for the properties of formFields
. As you can see, I'm using a counter to only run this.updatePanoCollection()
when all the files have been uploaded with api.uploadFieldFile
:
// formFields = { logo: { file: ... }, thumbnail: { file: ... } }
let toUploadCount = 0
let uploadedCount = 0
Object.entries(formFields).forEach(([key, upload]) => {
if (!upload || !upload.file) return
toUploadCount++
api.uploadFieldFile('logo', upload.file).then(downloadUrl => {
formFields[key] = downloadUrl
const updatePayload = {
id: this.currentPanoCollection.id,
data: formFields
}
uploadedCount++
if (toUploadCount === uploadedCount) {
// This only runs once right now
return this.updatePanoCollection(updatePayload)
}
}).then(() => {
// But this runs twice. It should only run once.
}).catch(err => this.handleError(err))
})
Now the problem is that the code inside .then()
runs twice.
How to change this code so it only runs once (after all the files have been uploaded)?