I am trying to upload image to firebase. I can successfully upload the image . But the problem is i cannot get the uploaded image’s url. because the promise takes longer and my data passes to the db faster than the image;s url response. here is my code:
post_news(form: NgForm){
this.title = form.value.title;
this.content = form.value.content;
this.category = form.value.category;
console.log(this.capturedimage1);
if(this.capturedimage1 !== ''){
//console.log('captured image is not empty');
let storageRef = firebase.storage().ref();
const filename = Math.floor(Date.now() / 1000);
const imageRef = storageRef.child(`images/${filename}.jpg`);
imageRef.putString(this.capturedimage1, firebase.storage.StringFormat.DATA_URL).then(function(data){
this.imageref1 = data.downloadURL;
let alert = this.alertCtrl.create({
title: 'uploadedimage',
subTitle: data.downloadURL,
buttons: ['dismiss']
});
alert.present();
});
console.log(this.imageref1) -->this returns empty string...but the image is uploaded to firebase.
}
based on my google search…they say this is a promise and it is asynchronous(will not wait till it get the response) . if that’s the case how can i get the uploaded image’s downloadURL.