找不到变量:getDownloadURL 在本机 firebase 中反应

Can't find variable: getDownloadURL in react native firebase

我已经从 firebase 文档本身复制并粘贴了代码,但我仍然收到此错误:

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

从“@react-native-firebase/storage”导入存储;

const pickImageAndUpload = async () => { 试试{

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

}

不知道为什么会出现这个错误,我到处找了下,其他人的代码都没有出现这个错误,求大神指教

可以查看官方文档here

如果您使用的是 React Native Firebase,那么 getDownloadURL() is a method on StorageReference 而不是函数(就像在 Modular SDK 中一样)。尝试重构代码,如下所示:

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