Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise return data cannot be assigned to class variable?

I am trying to assign the promise returned value to a variable but it is not assigning. I am still getting empty string when I console.log(this.imageref1). The objective here is to get the data.downloadURL which will be returned after uploading the photo to the firebase. I need the downloadedURL. to store it in my database for the image path. I am stuck here, my database gets empty data for the downloadURL.

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`);

    let uploaded_file_path = `images/${filename}.jpg`;      


    imageRef.putString(this.capturedimage1, firebase.storage.StringFormat.DATA_URL).then(data=>{

        this.imageref1 = data.downloadURL;

    });


    } 
}
like image 407
Manas Avatar asked Feb 15 '26 04:02

Manas


1 Answers

var that = this;
imageRef.putString(this.capturedimage1, firebase.storage.StringFormat.DATA_URL)
.then(data=>{      
    that.imageref1 = data.downloadURL;
    return Promise.resolve(data.downloadURL);
})
.then(function(url){
    //make the db call.
});
like image 63
gvmani Avatar answered Feb 16 '26 16:02

gvmani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!