Quick explanation of this block: I have a files
object, which is all the files I'm uploading, then I have a signedUrls
object which has all of the signed URLs from an earlier S3 function. The objects have matching indexes.
The first axios.put
uploads the file, and the second axios.post
saves the file key to my DB. (I don't want to save it to my DB unless it was uploaded successfully, hence the axios.post
's location in the callback.)
The files are uploading just fine, but the fileId
isn't looping properly and typically saves the same fileId
over and over again. I.e., if I upload five files, they'll upload to S3, but they'll all have the same id in my DB. Ideas why this is happening?
fileIds = {"1": "someFileId", "2": "someOtherId" }
for (let i = 0; i < files.length; i++) {
axios.put(signedUrls[i], files[i], config).then(res => {
axios.post('https://myapi.com/add-file', {
fileId: fileIds[i]
}).then(res => {
// success
});