Unable to get URL from Firebase's getDownloadURL
Stern Shaw:
I'm using Firebase storage to store images into it, the images are easily uploaded to the storage, but when I try to get the url of the image form Promise, it doesn't return any url.
const imageSaveHandler = (e) => {
e.preventDefault();
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on(
"state_changed",
(error) => {
console.log(error);
},
() => {
return storage
.ref(`images/${image.name}`)
.getDownloadURL()
.then((url) => {console.log(url)})
.catch((err) => console.log(err));
}
);
};
Ethan Lipkind:
uploadTask.on
According to the example in https://firebase.google.com/docs/storage/web/upload-files, the handler currently used as the third parameter should be changed to the fourth parameter :
var uploadTask = storageRef.child('images/rivers.jpg').put(file);
// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
});