skip to Main Content

I initialise my application this way and it work perfectly well as i can use the authentification.

import { initializeApp} from "firebase/app";
import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';
import { getAnalytics } from "firebase/analytics";
import { GoogleSignin } from '@react-native-google-signin/google-signin';
import { requestTrackingPermissionsAsync } from "expo-tracking-transparency";
import { Settings } from "react-native-fbsdk-next";

const firebaseConfig = {
  apiKey: "",
  authDomain: "-..",
  projectId: "-",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: ""
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

However, when i try to use the firebase storage with this function

import storage from '@react-native-firebase/storage'

export const UploadFile = async(file_uri,name,bucket) => {
    return new Promise((resolve, reject) => {
        try{

            const parts = file_uri.split(/[/\]/)
            const basename = parts[parts.length - 1]

            if(bucket){
                bucket = firebase.app().storage('gs://'+bucket+'.appspot.com').ref(basename)
            }else{
                bucket = storage().ref(basename)
            }

            bucket.putFile(file_uri)
            resolve(bucket)
        }catch(error){
            console.log('Error when uploading the file '+error)
            reject(error)
        }
    });
}

I get the error

No Firebase App ‘[DEFAULT]’ has been created – call firebase.initializeApp()

for the line bucket = storage().ref(basename)

I tried to pass it app as argument as it’s a possible argument but it didn’t work either.
I tried other solution found on stackoverflow, github, reddit, etc without success either

Thank you for your help

2

Answers


  1. Chosen as BEST ANSWER

    I don't know why the firebase doc is never up to date and the working solution are never the ones on the doc. Anyway, the solution : Get the storage this way

    import { getStorage } from 'firebase/storage';
    
    const app = initializeApp(firebaseConfig);
    const storage = getStorage(app)

    And Upload function :

    import { ref, uploadBytes } from 'firebase/storage';
    import { storage } from '../firebase';
    import { path } from '../classes/path';
    import { file } from '../classes/file';
    
    export const UploadFile = async (file_uri, path='', unique='') => {
        try {
            // Get the file name
            const parts = file_uri.split(/[/\]/);
            const basename = parts[parts.length - 1];
    
            const name = file.uniqueName(basename,unique)
            if(path)
                name = path.join(path,name)
    
            // Get the file content
            const response = await fetch(file_uri);
            const blob = await response.blob();
    
            const storage_ref = ref(storage, name);
    
            // Write the file in firebase
            await uploadBytes(storage_ref, blob)
    
            return storage_ref;
        } catch (error) {
            console.error('Error when uploading the file ', error);
            throw error;
        }
    };


  2. try this

    import { initializeApp} from "firebase/app";
    import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
    import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';
    import { getAnalytics } from "firebase/analytics";
    import { GoogleSignin } from '@react-native-google-signin/google-signin';
    import { requestTrackingPermissionsAsync } from "expo-tracking-transparency";
    import { Settings } from "react-native-fbsdk-next";
    
    const firebaseConfig = {
      apiKey: "",
      authDomain: "-..",
      projectId: "-",
      storageBucket: "",
      messagingSenderId: "",
      appId: "",
      measurementId: ""
    };
    
    // Initialize Firebase
    export const app = initializeApp(firebaseConfig);//export was missing
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search