I have 3 methods like below. _uploadImages()
takes images
array of objects. I want the _uploadImages()
to complete first and returns all the urls of uploaded images. Then I want to pass those urls to another method to save on another server.
But the problem is
_uploadImages()
returns an empty array immediately.
_uploadImages(images) {
let imageUrls = [];
images.map((image, index) => {
let formData = new FormData();
let file = {
uri: image.path,
type: image.mime,
name: image.path.substring(image.path.lastIndexOf('/') + 1),
size: image.size
}
formData.append('file', file);
formData.append('thumb', 'questions');
api
.uploadImage(formData)
.then((response) => {
if (response.data != null) {
let img = {
name: 'Image ' + (++index),
url: response.data.body.file,
type: 'image'
};
imageUrls.push(img);
}
//console.log(imageUrls);
})
.catch((error) => {
console.log(error);
});
});
return JSON.stringify(imageUrls);
}
_saveQuestion(postData) {
console.log(postData);
api
.postQuestion(postData)
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
}
_onQuestionFormSubmit() {
let images = this.state.images;
let imageUrls = this._uploadImages(images);
console.log(imageUrls);
let postData = {
medias: imageUrls
}
this._saveQuestion(postData);
}
Can anyone give some idea on this?