找不到变量:在反应原生火库中获取下载网址



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

警告可能未经处理的承诺拒绝(id:0(:ReferenceError:找不到变量:getDownloadURL

从"@反应天然火球/储存";;

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

}

我不知道为什么会出现这个错误,我到处都找过了,这个错误在任何其他人的代码中都没有发生,请帮助我

你可以在这里查看官方文件

如果您使用的是React Native Firebase,那么getDownloadURL()是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)
});
});

最新更新