问题
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