繁体   English   中英

Expo firebase 7.9.0 无法获取downloadUrl

[英]Expo firebase 7.9.0 can't get downloadUrl

我的方法确实设法将图像从 expo 加载到 firebase 存储,但我似乎无法下载 URL。

const uploadImage = async (uri) => {
const uniqid = () => Math.random().toString(36).substr(2, 9);
const ext = uri.split('.').pop(); // Extract image extension
const filename = `${uniqid()}.${ext}`; // Generate unique name
const response = await fetch(uri);
const blob = await response.blob();

var ref = firebase
  .storage()
  .ref()
  .child('images/' + filename);
ref.getDownloadURL().then((url) => console.log(url));
return ref.put(blob);

};

这是我得到的错误

FirebaseStorageError {

“code_”:“storage/object-not-found”,“message_”:“Firebase 存储:Object 'images/gebwu7tnh.jpg' 不存在。”,“name_”:“FirebaseError”,“serverResponse_”:“{ “错误”:{“代码”:404,“消息”:“未找到。 无法获取对象”,“状态”:“GET_OBJECT”} }”

这是我发现帮助我研究的东西。 我确实重构了我的 firebase 以使用更高阶的组件。 这是我的 firebase 方法。

 uploadImageAsync: async (uri) => {
const uniqid = () => Math.random().toString(36).substr(2, 9);
const ext = uri.split('.').pop(); // Extract image extension
const filename = `${uniqid()}.${ext}`; // Generate unique name
const ref = firebase
  .storage()
  .ref()
  .child('images/' + filename);
const blob = await new Promise((resolve, reject) => {
  const xhr = new XMLHttpRequest();
  xhr.onload = function () {
    resolve(xhr.response);
  };
  xhr.onerror = function (e) {
    console.log(e);
    reject(new TypeError('Network request failed'));
  };
  xhr.responseType = 'blob';
  xhr.open('GET', uri, true);
  xhr.send(null);
});

const snapshot = await ref.put(blob);

blob.close();
const imgUrl = await snapshot.ref.getDownloadURL();
console.log(imgUrl);
return imgUrl;

}, };

这是我在组件中实现它的方式

 const setImage = async (uri) => {
try {
  return await firebase.uploadImageAsync(uri);
} catch (error) {
  console.log(error);
}

};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM