0
votes

I'm try to upload in image to Firebase storage and then get the download URL from storage in order to save it to my database.

The commented out line works so I know its a problem with the "getDownloadURL" function or either in my storage reference. I have a similar function that works with a shorter reference (userId + '/' + profilePicture).

let returnVal;

const image = 'data:image/jpeg;base64,' + result;

const pictures = firebase.storage().ref(userId + '/' + newPostKey +'/' + 'pictures');
pictures.putString(image, `data_url`);

//firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set("ifni");

firebase.storage().ref(userId + '/' + newPostKey + '/' + pictures).getDownloadURL().then(function (url) {
  // Execute (unknown)
  returnVal = url;

  firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set(returnVal);
})
2

2 Answers

1
votes

You can get the url from the callback promise ans mentioned in this doc

pictures.putString(image, `data_url`).then(function(snapshot) {
  console.log('Uploaded a data_url string!');
  var url = snapshot.downloadURL;
  //add it to firestore
});
0
votes

You can do something like below

storageRef = firebase.storage().ref(userId + '/' + newPostKey +'/' + 'pictures');
    parseUpload = storageRef.putString(image, 'data_url');
    parseUpload.on('state_changed', (_snapshot) => {
          },
            (_err) => {
              // handle error here
            },
            (success) => {
              let url = parseUpload.snapshot.downloadURL
            });
firebase.database().ref('Posts/' + userId + '/' + newPostKey).child('photoURL').set(url);