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

164
我有一个连接到两个应用程序的 Firebase 数据库,一个是 iOS 应用程序,另一个是使用 Node.js 编写的 Web 应用程序。Web 应用程序是一个基本算法,用于将数据设置到数据库中。每当我运行算法时,都会遇到以下错误信息:Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp().请问有人可以帮忙解决吗?
29个回答

2

2

在处理iOS时遇到了相同的错误。希望您已经使用pods安装了Firebase。您需要执行以下操作。 打开Xcode并打开AppDelegate.m文件,然后导入

#import "FIRApp.h"

现在在didFinishLaunchingWithOptions代理方法中调用configure方法。
  [FIRApp configure];

现在运行你的应用。它应该可以工作。这里有文档链接

1
这个错误是因为您在成功初始化 Firebase 之前尝试使用 Firebase 函数。
解决方法:
将要调用的函数放置在 setInterval 块内,以便只有在应用程序被初始化后才调用该函数。
 let isFirebaseAppDefined = false;
    setInterval(() => {
      if (!isFirebaseAppDefined) {
        if (firebase.app()) {

          // Function that needs to run after the initialisation comes here
          // Example re-captcha verifier or other auth function

          isFirebaseAppDefined = true;
        }
      }
    }, 100);

1

我在更新React Native版本后,在iOS上遇到了这个错误,可以在文件 ios/{AppName}/AppDelegate.m 的方法 didFinishLaunchingWithOptions 中添加以下指令来解决:

   if ([FIRApp defaultApp] == nil) {
     [FIRApp configure];
   }

它应该像这样: 输入图像描述

1
另一种解决方案在这里。

使用APP_INITIALIZER。

https://angular.io/api/core/APP_INITIALIZER

export function appInitializer() {
  return () => firebase.initializeApp(firebaseConfig);
}

...
@NgModule({
 ...
 providers: [{
   provide: APP_INITIALIZER,
   useFactory: () => appInitializer
   multi: true
  }]
 })
export class AppModule {}

1
在Jade中,您可以这样调用:firebase.initializeApp(config); 在函数的开头
script.
    function signInWithGoogle() {
        firebase.initializeApp(config);
        var googleAuthProvider = new firebase.auth.GoogleAuthProvider
        firebase.auth().signInWithPopup(googleAuthProvider)
        .then(function (data){
            console.log(data)
        })
        .catch(function(error){
            console.log(error)
        })
    }

我认为这与所述的问题无关。 - akauppi
@akauppi 这是相关的。我在 Angular 中使用了这个解决方案。我只是将它包含在函数的开头,而不是写在应用程序模块中。 - Mayank Kataria

0

步骤1:

使用npm

npm install --save @react-native-firebase/app

使用Yarn

yarn add @react-native-firebase/app

步骤2: 在 https://console.firebase.google.com/ 中生成Android凭据。 “Android包名称”必须与本地项目的包名称匹配,可以在项目中的/android/app/src/main/AndroidManifest.xml文件中的manifest标记内找到。

下载google-services.json文件并将其放置在项目的以下位置:/android/app/google-services.json。

步骤3: 然后在/ android / build.gradle文件中将google-services插件添加为依赖项: classpath 'com.google.gms:google-services:4.3.13'

最后,在/ android / app / build.gradle文件中添加以下内容以执行该插件: apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' // <- 添加此行

就这样....


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

0
使用以下代码: https://github.com/abhi3700/My_Learning_Databases/tree/main/Firebase#initialize-firestore
const fs = require("firebase-admin");

const serviceAccount = require("../../../helper/serviceAccountKey.json");

let db = null;

// initialize firebase
if (!fs.apps.length) {
  fs.initializeApp({
    credential: fs.credential.cert(serviceAccount),
    // name: "zippy-mvp2",
  });

  db = fs.firestore();

  // allow undefined values.
  db.settings({
    ignoreUndefinedProperties: true,
  });
} else {
  db = fs.app().firestore();
}

0

在 app.module.ts 中使用 Initialize app

import { environment } from 'src/environments/environment';
firebase.initializeApp(environment.firebase);

这将清除错误。
您可以使用 firebase.database() 而不会出现任何错误。


0

我遇到了同样的问题。当我尝试将我的Flutter Web应用添加到Firebase时,我将Google在设置过程中给我的脚本标签复制并粘贴到了我的index.html文件中。但即使我在main方法中添加了以下行代码,这对我也没有起作用:

WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());

使用此处发布的格式中的脚本,我使其工作: https://firebase.flutter.dev/docs/installation/web 如果有其他人遇到相同的问题并盲目复制Firebase设置中Google提供的脚本标签...这对我很有帮助。只需按FlutterFire发布的格式进行即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接