Expo / Firestore, can't upload image using uploadBytesResumable

Viewed 15

I have this Expo project where I have to upload a profile image. Updating everything works fine but when I want to upload the image, to Firebase Storage, sometimes it uploads just fine but sometimes the app crashes, no error message, no nothing. It happens in both, the simulators and devices (iOS and Android), between 18% to 80% of completion.

Here's the upload function and the import statement as well:

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();
            });
        }
        );
0 Answers
Related