1

What I am trying to do is upload multiple files with this code and create array location filled up with location. But when i try to console.log location after the for loop, it returns empty result. Can any one help me with this problem? Uploading S3 worked very well. Just failed to make an array 'location'. Try to solve this problem for 4hours in a row... I would appreciate your help.

var arr = []
const uploadToS3 = async() =>{
var i = 0;
for (i;i<images.length;i++){
  var file =  
  {
    name: brandInput.value+"-"+productNameInput.value+"-"+i+".jpg",
    type: "image/jpeg",
    uri: images[i].uri
  }
  RNS3.put(file, options).then(response => arr.push(response.body.postResponse.location));
}

}

boriii
  • 87
  • 6
  • 1) You're not initializing `i` to `0` and 2) You're performing asynchronous logic within a synchronous loop. The loop will terminate before the asynchronous logic finishes. – Taplar Dec 04 '20 at 22:05
  • See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – jarmod Dec 04 '20 at 22:06
  • I have changed my full code I'll take a look at your links Thanks a lot. God Bless You Guys – boriii Dec 04 '20 at 22:10
  • can you guys take a look at my code again? i try to under stand async await with codesandbox and other examples but i got stuck again – boriii Dec 04 '20 at 22:46
  • First make `uploadToS3` to be async function. Then move the return statement to be in front of this `RNS3.put(file, options).then(response => {` so it should become `return RNS3.put(file, options).then(response => {` and remove the `return arr` code and it should work. – George Pandurov Dec 04 '20 at 22:53

0 Answers0