【问题标题】:Expo firebase 7.9.0 can't get downloadUrlExpo firebase 7.9.0 无法获取 downloadUrl
【发布时间】:2020-10-13 05:11:03
【问题描述】:

我的方法确实设法将图像从 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_": "存储/未找到对象", "message_": "Firebase 存储:对象 'images/gebwu7tnh.jpg' 不存在。", "name_": "FirebaseError", “服务器响应_”:“{ “错误”: { “代码”:404, "message": "未找到。无法获取对象", “状态”:“GET_OBJECT” } }"

【问题讨论】:

  • 您的代码似乎为文件名生成了一个唯一 ID,然后尝试确定该 ID 的下载 URL。 Firestore 存储中似乎不太可能存在该唯一 ID 的文件,这就是您收到错误消息的原因。
  • 谢谢你是对的,我确实找到了解决方案,我会尽快发布

标签: javascript firebase react-native expo firebase-storage


【解决方案1】:

这是我发现的对我研究有帮助的东西。我确实重构了我的 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);
}

};

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2021-09-22
    • 2021-03-18
    相关资源
    最近更新 更多