Reputation: 5
I am using firebase storage to store my image to it , image is upload to the storage easily ,but when i try to get the url of the image form promise, it returns no 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));
}
);
};
Upvotes: 0
Views: 352
Reputation: 1156
it looks like the handler you currently have as the third argument to uploadTask.on
should be the fourth argument instead, according to this example from https://firebase.google.com/docs/storage/web/upload-files:
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);
});
});
Upvotes: 1