yogesh kumar
yogesh kumar

Reputation: 103

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:

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

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

Upvotes: 1

Views: 599

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50830

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)
  });
});

Upvotes: 1

Related Questions