首页 > 解决方案 > React Native - 使用 @react-native-firebase/storage 将图像上传到 Firebase - 没有创建 Firebase 应用“[DEFAULT]”

问题描述

将图像上传到 Firebase 时出现以下错误:

错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 firebase.initializeApp()

这是我的代码:

  1. 应用程序.js

    import * as Firebase from 'firebase';
    
    componentDidMount() {
        Firebase.initializeApp(firebaseConfig);
    }
    
  2. Profile.js

    import * as Firebase from 'firebase';
    import rnFb from '@react-native-firebase/storage';
    
    uploadImage = localUri =>
    new Promise((resolve, reject) => {
        const localUri2 = Platform.OS === 'ios' ? localUri.replace('file://', '') : localUri;
        const fbUri = Firebase.storage().ref();
        rnFb().ref(localUri2).putFile(fbUri)
        .then(
            () => { resolve(); }
        )
        .catch(
            (e) => { reject(e); }
        );
    });
    

它在生产线上失败了.putFile

我不明白问题出在哪里,因为我正在.initializeApp()打电话App.js

更新 12/21console.log(Firebase.apps.length);之前添加rnFb().ref(localUri2).putFile(fbUri)了,输出为 1 ......确实很奇怪。

...如果我完全按照错误的要求进行操作并在收到错误错误firebase.initializeApp()之前立即调用:rnFb().ref(localUri2).putFile(fbUri)

Firebase: Firebase App named '[DEFAULT]' already exists

帮助!!

标签: firebasereact-nativefirebase-storagereact-native-firebase

解决方案


My understanding is that the Firebase SDK used internally inside @react-native-firebase is independent of the ordinary Firebase SDK from firebase.

It can be exposed using:

import firebase from '@react-native-firebase/app';
// OR
import { firebase } from '@react-native-firebase/storage';

Applying these changes (and simplifying your code), leaves you with the following:

import storage, { firebase } from '@react-native-firebase/storage';

// can possibly be somewhere else
firebase.initializeApp(firebaseConfig);

uploadImage = localUri => {
  const localUri2 = Platform.OS === 'ios' ? localUri.replace('file://', '') : localUri;
  return storage().ref('/path/to/upload/to').putFile(localUri2)
}

Rather than use "client initialization" using firebase.initializeApp(), you can also use "native initialization" for Android and iOS.


推荐阅读