i trying able upload multiple files , have them sent cloudinary's api. looping through each image, attaching , uploading it. however, code looping through images , uploading first image on again amount of files:
let uploadrequest = superagent.post(url) (let i=0; i<files.length; i++){ uploadrequest.attach('file', files[i]) console.log('file attached', files[i]) object.keys(params).foreach((key) => { uploadrequest.field(key, params[key]) }) console.log('params attached', files[i]) uploadrequest.then((res) => { console.log('upload complete: '+json.stringify(res.body)) const uploaded = res.body const imageurl = res.body.secure_url let updatedarr = this.state.images.slice(); updatedarr.push(imageurl); this.setstate({ images: updatedarr }) }) .catch((error) => { console.log(error) return }) } }
how can rewrite want? confused why keeps happening
you can't have asynchronous code in middle of loop. loop not wait request finish.
move separate function , use loop call function.
var uploadfile = function(file){ //do stuff } for(var i=0; i<files.length; i++){ uploadfile(files[i]); }
Comments
Post a Comment