Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't find variable: getDownloadURL in react native firebase

I have copied and pasted code from the firebase documentation itself, still I am getting this error

error is

WARN Possible Unhandled Promise Rejection (id: 0):
ReferenceError: Can’t find variable: getDownloadURL

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import storage from "@react-native-firebase/storage";

const pickImageAndUpload = async () => {
try {

    launchImageLibrary({
        quality: 0.5
    }, (fileobj) => {
        console.log(fileobj.assets[0].uri);

        const uploadTask = storage().ref().child(`/userprofile/${Date.now()}`).putFile(String(fileobj.assets[0].uri))

        uploadTask.on('state_changed',
            (snapshot) => {
                const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                if (progress == 100) alert('image uploaded')
            },
            (error) => {
                // Handle unsuccessful uploads
                alert('error uploading image')
            },
            () => {
                getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
                    setImage(downloadURL)
                });
            }
        );
    })

} catch (err) {
    alert(err)
    console.log(err);
}
}

I don’t know why this error is coming, I have searched everywhere, this error does not occur in any other person code, please help me

You can check the official documentation here

>Solution :

If you are using React Native Firebase then getDownloadURL() is a method on StorageReference and not a function (like in Modular SDK). Try refactoring the code as shown below:

const storageRef = storage().ref().child(`/userprofile/${Date.now()}`)

const uploadTask = storageRef.putFile(String(fileobj.assets[0].uri))

uploadTask.on('state_changed', (snapshot) => {
  const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
  if (progress == 100) alert('image uploaded')
}, (error) => {
  // Handle unsuccessful uploads
  alert('error uploading image')
}, () => {
  storageRef.getDownloadURL().then((downloadURL) => {
    setImage(downloadURL)
  });
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading