Luis Pinho
Luis Pinho

Reputation: 55

Ionic 4 - Firebase - Storage not sending data to realtime Database

I'm uploading images to Firebase storage, and then i want them to go to the real time database, this code was working in ionic v3, but now it seems something is wrong since the data goes to the storage but not to the database.

    createPost(picture: string): Promise<any> {

        firebase.storage().ref('/home/')
            .child('picture.jpg')
            .putString(picture, 'base64', { contentType: 'image/jpg' })
            .then((savedPicture) => {
                firebase.database().ref('Home').push({
                    picture: savedPicture.downloadURL
                }).then(() => {
                    alert('Sucess');
                    this.navCtrl.navigateRoot('/home');
                })
            });
        return
}

Upvotes: 0

Views: 238

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

The download URL of the new upload is no longer available as savedPicture.downloadURL. You will need call getDownloadURL() on the storage reference after the upload completes:

let ref = firebase.storage().ref('/home/').child('picture.jpg');
 ref.putString(picture, 'base64', { contentType: 'image/jpg' })
    .then((savedPicture) => {
        ref.getDownloadURL().then((url) => {
          firebase.database().ref('Home').push({
            picture: url
          }).then(() => {
            alert('Sucess');
            this.navCtrl.navigateRoot('/home');
          })
        })
    });

Also see:

Upvotes: 2

Related Questions