javascript 在react native expo中将图像上传到Firebase时未处理的Promise拒绝?

wfveoks0  于 2023-04-19  发布在  Java
关注(0)|答案(2)|浏览(100)

我得到:
可能的未处理Promise拒绝(id:0):TypeError:undefined不是对象(正在计算“ref._location')`
我甚至把裁判改成

import { ref as refstore, uploadBytesResumable, getDownloadURL } from "firebase/storage";

这样就不会互相碰撞。并改变图像斑点。我被困在这里不知道下一步该做什么。

const uploadImage = async () => {
  const blobimage = await new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.onload = function () {
      resolve(xhr.response);
    };
    xhr.onerror = function () {
      reject(new TypeError("Network request failed"));
    };
    xhr.responseType = "blob";
    xhr.open("GET", imageUri, true);
    xhr.send(null);
  })

  const metadata = {
    contentType: 'image/jpeg'
  };
  // I think something is wrong in the below line **storageRef** <------------
  const storageRef = refstore(storage,`images/${Date.now()}`);
  const uploadTask = uploadBytesResumable(storageRef, blobimage, metadata);

  uploadTask.on('state_changed',
    (snapshot) => {
      const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
      console.log('Upload is ' + progress + '% done');
      switch (snapshot.state) {
        case 'paused':
          console.log('Upload is paused');
          break;
        case 'running':
          console.log('Upload is running');
          break;
      }
    },
   
    
  );
};
irlmq6kh

irlmq6kh1#

需要通过getStorage预建函数定义storage变量。
就像这样:

import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";

const app = initializeApp(firebaseConfig);
const storage = getStorage(app);

初始化Firebase存储后,您可以将存储对象传递给refstore:

const storageRef = refstore(storage, `images/${Date.now()}`);
vkc1a9a2

vkc1a9a22#

从配置文件导入时,不要忘记在导入时使用{}。

相关问题