firebase snapshot.downloadURL is undefined after successfully uploading an image

跟風遠走 提交于 2019-11-30 19:52:58

问题


I have this code in my app, that I use to upload an image and get its url so that i can save it in the database, the image is in base64 format and the upload is successfull as i can see in console.log(snapshot); output, and by checking also in my firebase storage however, the downloadUrl property of snapshot is undefined i dont know why. This is not the way it was supposed to work

storage.$putString(b64, 'data_url', {contentType:'image/jpg'}).$complete(function(snapshot) {
        console.log(snapshot);
        item.avatarUrl=snapshot.downloadURL;
        agents.$add(item).then(function(ref) {
        });

    });     

回答1:


Use snapshot.ref.getDownloadURL()




回答2:


Novomber 2019 update from the official documentation of firebase :

function(){
      uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
        console.log('File available at', downloadURL);
      });

    });

Here is the source from firebase documentation




回答3:


I am using angularfire2 5.0.0-rc11 and facing the same issue. I modified the code as follows:

storage.$putString(b64, 'data_url', {contentType:'image/jpg'}).$complete(function(snapshot) {
    console.log(snapshot);
    //item.avatarUrl=snapshot.downloadURL;
      // changed to:
      snapshot.getDownloadURL()
      .then( downloadUrl => {

        item.avatarUrl=downloadUrl
        agents.$add(item).then(function(ref) {
        });
      })
      .catch( error => {
        console.log(error);
        //catch error here
      });

});    


来源:https://stackoverflow.com/questions/50563415/firebase-snapshot-downloadurl-is-undefined-after-successfully-uploading-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!