firebase Expo / Firestore,无法使用uploadBytesResumable上传图像

s4n0splo  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个Expo项目,我必须上传一个个人资料图片。更新一切正常,但当我想上传图片到Firebase存储,有时上传正常,但有时应用程序崩溃,没有错误消息,什么都没有。它发生在模拟器和设备(iOS和Android),完成18%到80%。
下面是upload函数和import语句:

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

const uploadImage = async (theImage) => {

        const response = await fetch(theImage);
        const file = await response.blob();
        const storageRef = ref(storage, `images/${signedUser.uid}`);
        const uploadTask = uploadBytesResumable(storageRef, file);

        // Listen for state changes, errors, and completion of the upload.
        uploadTask.on('state_changed',
        (snapshot) => {
            // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
            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;
            }
        }, 
        (error) => {
            console.log("Error: ", error.message)
        }, 
        () => {
            // Upload completed successfully, now we can get the download URL
            getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
                /*console.log('File available at', downloadURL);
                navigation.navigate({
                    name: 'Perfil',
                    params: { changed: true },
                    merge: true,
                  });*/
                navigation.goBack();
            });
        }
        );
gv8xihay

gv8xihay1#

您可以修补您的@firebase/storage scope软件包以按预期上传映像,而不会崩溃!

首先,导航到node_modules/@firebase/storage/dist/index.esm2017.js,然后注解掉if语句,以防止它有时清除超时(这似乎是导致崩溃的原因,正如这里所提到的。

function clearGlobalTimeout() {
    // if (globalTimeoutId) {
    //     clearTimeout(globalTimeoutId);
    // }
}

现在您已经对node_module进行了更改,您可以使用yarn的Patch Package使这些更改具有粘性!

除非创建了补丁文件,否则对@firebase/storage node_module的更改将不会具有粘性-您必须告诉您的应用在新安装每个node_module后使用该补丁文件。甚至可以通过将postinstall脚本添加到package.json,在生产中通过EAS构建部署这些更改

"scripts": {
    "postinstall": "patch-package"
}

要开始修补您自己的@firebase/storage,请使用yarn's Patch Package模块。🍀

相关问题