sarfrazanwar
sarfrazanwar

Reputation: 393

How to get download url for a Image from Firebase Storage?

I am trying to upload an image to firebase storage after that I am trying to get the download url of image but it's not working. Below is the code :

const response = await fetch(selectedImage.uri);
const file = await response.blob();

const storageRef = ref(storage, `profile/${currentUser.email}`);
uploadBytes(storageRef, file).then( (snapshot) => {
  console.log('uploaded');
  getDownloadURL(storage).then( url => console.log(url)); // tried this outside of then block as well
});

the file is uploaded and accessible through console but getDownloadURL throws error :

[Unhandled promise rejection: TypeError: ref._throwIfRoot is not a function. (In 'ref._throwIfRoot('getDownloadURL')', 'ref._throwIfRoot' is undefined)]

Upvotes: 2

Views: 4099

Answers (1)

Charles Fries
Charles Fries

Reputation: 365

Instead of passing storage to getDownloadURL(), pass snapshot.ref.

const response = await fetch(selectedImage.uri);
const file = await response.blob();

const storageRef = ref(storage, `profile/${currentUser.email}`);
uploadBytes(storageRef, file).then( (snapshot) => {
  console.log('uploaded');
  getDownloadURL(snapshot.ref).then( url => console.log(url));
});

Upvotes: 3

Related Questions