【问题标题】:React-Native: "FirebaseError: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)"React-Native:“FirebaseError:没有创建 Firebase App '[DEFAULT]' - 调用 Firebase App.initializeApp() (app/no-app)”
【发布时间】:2019-12-04 11:09:07
【问题描述】:

我正在构建一个 React Native 应用程序,目前正在尝试通过 Firebase Auth 实现身份验证注册系统。我已经按照网站上的指南/文档来设置 Firebase 配置文件。我运行应用程序,输入电子邮件和密码,当我点击注册按钮时,我收到以下错误:

FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).

我在 google 上搜索了可能的修复方法,但似乎没有任何效果。我已经按照我看过的文档/教程初始化了 Firebase。希望有人可以对此事有所了解。我将在下面包含配置文件代码,XXXX 来替换我的实际配置信息

firebase.js

import * as firebase from 'firebase/app';
import "firebase/auth";

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

firebase.initializeApp(firebaseConfig);

api.js

export function registerUser({email, password}) {
  firebase
    .auth()
    .createUserWithEmailAndPassword(email, password)
    .catch(function(error) {
      console.log(error);
    });
};

编辑/回答

正如下面 cmets 所提供的帮助,正确的方法是通过 React-Native-Firebase 实施/导入。链接在 cmets 中。

【问题讨论】:

  • 你正在使用 react-native-firebase 包?通过转化酶?
  • 如果你使用 react-native-firebase 你不需要使用配置!您必须将 json 文件、plist 文件放在项目的本机部分!对于 Auth,您也必须安装 firebase/auth!
  • 我离开的指南只是使用了firebase,如上面的代码所示。它通过 Firebase 控制台而不是 Android/iOS 使用网络设置
  • import * as firebase from 'firebase/app';导入“firebase/auth”;如果您使用 FIrebase(来自 npm 的 JS 包),它将无法工作。如果您希望 firebase 在 RN 应用程序上更好地工作,请使用 React-native-firebase
  • 啊,好吧。所以只是'npm i react-native-firebase'就这样?还是我也必须在 android build.gradle 文件中进行配置?

标签: android firebase react-native firebase-realtime-database firebase-authentication


【解决方案1】:

您似乎正在使用 Invertase Firebase 项目的语法,而实际上您正在使用来自 firebase npm 包的 firebase WEB SDK。

这是你应该做的:

here 安装 react-native-firebase

确保 pod 正常工作且自动链接正常工作,您已包含 json 和 plist 文件。!只有遵循这些您的项目才能构建,并且您可以使用您在问题中发布的语法!

【讨论】:

    【解决方案2】:

    你写你的配置如下:

    componetWillMount() {
    const config = {
    apiKey: “xxxxxxxxxxxxxxxxxxxxxxxx”,
    authDomain: “auth-bdcdc.firebaseapp.com 20”,
    databaseURL: “https://auth-bdcdc.firebaseio.com 7”,
    projectId: “auth-bdcdc”,
    storageBucket: “auth-bdcdc.appspot.com 2”,
    messagingSenderId: “xxxxxxxxxx”
    };
    firebase.initializeApp(firebaseConfig);
    }
    

    【讨论】:

      【解决方案3】:

      检查 ios 文件夹中是否缺少 GoogleService-Info.plist

      【讨论】:

        【解决方案4】:

        这就是我为解决它所做的。

        使用异步函数进行注册

        FirebaseConfig.js

        import firebase from 'firebase/app';
        
        
        const firebaseConfig = {
          apiKey: "XXXX",
          authDomain: "XXXX",
          databaseURL: "XXXX",
          projectId: "XXXX",
          storageBucket: "XXXX",
          messagingSenderId: "XXXX",
          appId: "XXXX",
          measurementId: "XXXX"
        };
        
        firebase.initializeApp(firebaseConfig); // It's expecting an objects
        
        

        注册为异步函数:

        Registration.js

        export async function Registration(email, password, lastName, firstName) {
          try {
            await firebase.auth().createUserWithEmailAndPassword(email, password);
            const currentUser = firebase.auth().currentUser;
            const db = firebase.firestore();
            db.collection("users").doc(currentUser.uid).set({
              email: currentUser.email,
              lastName: lastName,
              firstName: firstName,
            });
          } catch (err) {
            Alert.alert("There is something wrong!!!!", err.message);
          }
        }
        
        

        如果这些作品别忘了点赞。

        【讨论】:

          【解决方案5】:

          我认为需要在 AppDelegate.m 文件中进行以下设置...

           #import <Firebase.h>
            
              - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
              {
                [FIRApp configure];
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-09-09
            • 2022-11-07
            • 2021-07-13
            • 2021-06-12
            • 2021-07-25
            • 1970-01-01
            • 2021-05-13
            • 2021-07-21
            相关资源
            最近更新 更多