【问题标题】:Error: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp()错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 Firebase App.initializeApp()
【发布时间】:2017-03-26 13:53:42
【问题描述】:

我有一个链接到两个应用程序的 firebase 数据库,一个是 iOS 应用程序,另一个是用 node.js 编码的 Web 应用程序,这是一种将数据设置到数据库的基本算法。每当我运行我遇到的算法时-

错误:没有创建 Firebase 应用“[DEFAULT]” - 调用 Firebase App.initializeApp()。 在错误(本机) 在 R (/Users/dd/Desktop/Code/NODE/node_modules/firebase/app-node.js:22:335) 在 (/Users/dd/Desktop/Code/NODE/node_modules/firebase/app-node.js:20:68) 在 Object.c [作为数据库] (/Users/dd/Desktop/Code/NODE/node_modules/firebase/app-node.js:21:447) 在对象。 (/Users/dd/Desktop/Code/NODE/Bot.js:24:25) 在 Module._compile (module.js:570:32) 在 Object.Module._extensions..js (module.js:579:10) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12) 在 Function.Module._load (module.js:438:3) 在 Module.runMain (module.js:604:10) 运行时(bootstrap_node.js:394:7) 启动时(bootstrap_node.js:149:9) 在 bootstrap_node.js:509:3 dd-mac:NODE dd$

有人可以帮忙吗?

【问题讨论】:

    标签: javascript ios node.js firebase firebase-realtime-database


    【解决方案1】:

    当我更新 React Native 版本时,我在 ios 中遇到了这个错误,在方法中添加这条指令:didFinishLaunchingWithOptions from file:ios/{AppName}/AppDelegate.m

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

    它应该看起来像这样:

    【讨论】:

      【解决方案2】:

      您可能在应用初始化之前调用了firebase。所有对firebase 的调用都必须在之后 .initializeApp();

      firebase.initializeApp(config);
      var db = firebase.firestore();
      

      【讨论】:

      • 这是我的问题,但不是因为代码的逐行顺序,而是我的 IMPORTS 的顺序。我在初始化它的代码之前导入了使用 firebase 的代码。
      • 你的进口顺序是什么意思?我试过这个-import firebase from '@react-native-firebase/app'; 然后import "@react-native-firebase/database" 但它不起作用
      • 这也是我的问题,但对我来说,这是因为 Firebase 在我自己的 firebase.js 文件(从 firebase 导入)中初始化,并且在另一个模块中,我不小心导入了来自firebase 而不是我的(初始化的)./firebase
      【解决方案3】:

      我有同样的问题。当我尝试将我的 Flutter Web 应用程序添加到 Firebase 时,我将谷歌在设置过程中给我的脚本标签粘贴到我的 index.html 中。即使在我使用 main 方法中的以下行修改了 main.dart 之后,这对我也不起作用:

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

      使用此处发布的格式的脚本,我得到了它的工作: https://firebase.flutter.dev/docs/installation/web

      如果有其他人遇到同样的问题并盲目地复制 Google 在 Firebase 设置中为您提供的脚本标签......这对我有帮助。只需将其转换为 FlutterFire 发布的格式即可。

      【讨论】:

        【解决方案4】:

        这里有另一个解决方案。

        使用 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 {}
        

        【讨论】:

          【解决方案5】:

          如果你是 react native 并为 IOS 开发,那么我想你忘记了 firebase 模块的链接步骤。

          按照以下步骤..!

          打开您的 /ios/{projectName}/AppDelegate.m 文件,并添加以下内容:

          在文件顶部,导入 Firebase SDK:

          #import <Firebase.h>
          

          在您现有的didFinishLaunchingWithOptions 方法中,将以下内容添加到方法的顶部:

          - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Add this  --- \/
            if ([FIRApp defaultApp] == nil) {
              [FIRApp configure];
            }
            // Add me --- /\
            // ...
          }
          

          【讨论】:

            【解决方案6】:

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

            #import "FIRApp.h"
            

            现在在 didFinishLaunchingWithOptions 委托方法中调用 configure 方法

              [FIRApp configure];
            

            现在运行您的应用。它应该工作。这是文档link

            【讨论】:

              【解决方案7】:

              此错误是因为您在成功初始化之前尝试使用 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);
              

              【讨论】:

                【解决方案8】:

                在 app.module.ts 中使用 Initialize app

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

                这将清除错误。
                您可以使用 firebase.database() 没有任何错误

                【讨论】:

                  【解决方案9】:

                  如果你使用 Dart 和 Flutter

                  1. 将 firebase_core 依赖项添加到 pubspac.ymal。
                  2. 转到 main.dart
                  3. 导入'package:firebase_core/firebase_core.dart';

                  4.在main()中添加async

                  关注我的代码

                  void main() async {
                    WidgetsFlutterBinding.ensureInitialized();
                    await Firebase.initializeApp();
                    runApp(MyApp());
                  }
                  
                  class MyApp extends StatelessWidget {
                    var fsconnect = FirebaseFirestore.instance;
                  
                    myget() async {
                      var d = await fsconnect.collection("students").get();
                      // print(d.docs[0].data());
                  
                      for (var i in d.docs) {
                        print(i.data());
                      }
                    }
                  
                    @override
                    Widget build(BuildContext context) {
                      return MaterialApp(
                          home: Scaffold(
                        appBar: AppBar(
                          title: Text('Firebase Firestore App'),
                        ),
                        body: Column(
                          children: <Widget>[
                            RaisedButton(
                              child: Text('send data'),
                              onPressed: () {
                                fsconnect.collection("students").add({
                                  'name': 'sarah',
                                  'title': 'xyz',
                                  'email': 'sarah@gmail.com',
                                });
                                print("send ..");
                              },
                            ),
                            RaisedButton(
                              child: Text('get data'),
                              onPressed: () {
                                myget();
                                print("get data ...");
                              },
                            )
                          ],
                        ),
                      ));
                    }
                  }
                  

                  【讨论】:

                  • 这对你有用。我尝试了在 Main 中添加异步的相同方法。不解决问题。现在我关注link。链接也没有解决。收到错误 Firebase 未初始化。
                  • 那个解决方案对我也不起作用。根据此视频youtube.com/watch?v=Z0jFkP0A3B0,我正确地完成了所有操作,但不幸的是我仍然收到错误消息...
                  【解决方案10】:

                  我认为出现此错误是因为您在没有得到正确配置的相应 React 平台中使用类组件。 所以你在 componentWillMount() 中编写配置。

                  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”
                  };
                  

                  【讨论】:

                    【解决方案11】:

                    我找到了解决办法!

                    按照以下步骤操作:

                    Flutter Firebase and Android issue - unable to Initialise. Cannot find google-services.json with latest migration instructions executed

                    之后,执行:

                    flutter build apk --debug
                    flutter build apk --profile
                    flutter build apk --release
                    

                    然后,运行应用程序!它对我有用!

                    【讨论】:

                      【解决方案12】:

                      答案可能已经在某处给出,但这是我对这个可能因多种原因引发的错误的看法

                      1. 默认应用程序在另一个应用程序之后初始化。正确的做法是先初始化默认应用,然后再初始化其他应用。
                      2. firebase.apps.app() 在默认应用程序初始化之前被调用。这段代码基本上是返回默认的应用程序实例。由于它不存在,因此错误。
                      3. 最后,您要在应用初始化之前初始化其他 Firebase 服务,例如身份验证、数据库、Firestore 等。

                      【讨论】:

                      • 对我来说是#3。我将导出命名为{firebaseClient, dbClient,authClient,storageClient},其中各个服务定义为const dbClient = firebaseClient.firestore(); ...
                      • 没听说过1。会查!
                      【解决方案13】:

                      如果您正在启动一个 react-native 应用程序并看到此问题,那么您必须遵循 firebase 中列出的所有说明(当您设置 iOS/android 应用程序时)或说明@@ 987654321@问题

                      【讨论】:

                        【解决方案14】:

                        Flutter 网络

                        对我来说,当我在“发布”模式下运行我的应用程序时发生了错误

                        flutter run -d chrome --release

                        当我在 Firebase 主机上部署应用程序时

                        firebase deploy

                        解决方案

                        由于我在 index.html 中初始化了 Firebase,所以我不得不更改 firebase 和 main.dart.js 的实现顺序

                        <script>
                          var firebaseConfig = {
                          apiKey: "xxxxxxxxxxxxxxxxxxxxxx",
                          authDomain: "xxxxxxxxxxx.firebaseapp.com",
                          databaseURL: "https://xxxxxxxxxx.firebaseio.com",
                          projectId: "xxxxxxxxxxx",
                          storageBucket: "xxxxxxxx.appspot.com",
                          messagingSenderId: "xxxxxxxxxxx",
                          appId: "1:xxxxxxxxxx:web:xxxxxxxxxxxxx",
                          measurementId: "G-xxxxxxxxx"
                          };
                          // Initialize Firebase
                          firebase.initializeApp(firebaseConfig);
                          firebase.analytics();
                        </script>
                        
                        //moved below firebase init
                        <script src="main.dart.js" type="application/javascript"></script>
                        

                        【讨论】:

                        • 这解决了问题
                        【解决方案15】:

                        完整教程来源link

                        在@NgModule 之前使用initializeApp

                        import { NgModule } from '@angular/core';
                        import { BrowserModule } from '@angular/platform-browser';
                        import { RouteReuseStrategy } from '@angular/router';
                        
                        import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
                        import { SplashScreen } from '@ionic-native/splash-screen/ngx';
                        import { StatusBar } from '@ionic-native/status-bar/ngx';
                        
                        import { AppComponent } from './app.component';
                        import { AppRoutingModule } from './app-routing.module';
                        import { environment } from 'src/environments/environment';
                        import { AuthenticateService } from './services/authentication.service';
                        import { AngularFireAuthModule } from '@angular/fire/auth';
                        
                        import * as firebase from 'firebase';
                        
                        firebase.initializeApp(environment.firebase);
                        
                        @NgModule({
                          declarations: [AppComponent],
                          entryComponents: [],
                          imports: [
                            BrowserModule, 
                            IonicModule.forRoot(), 
                            AppRoutingModule,
                            AngularFireAuthModule
                          ],
                          providers: [
                            StatusBar,
                            SplashScreen,
                            AuthenticateService,
                            { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
                          ],
                          bootstrap: [AppComponent]
                        })
                        export class AppModule {}
                        

                        【讨论】:

                        • 不应使用特定于库的答案来回答与库无关的问题。
                        • @mesqueeb 这可能是一个只发生在角度的错误,所以回答特定语言就可以了。
                        【解决方案16】:

                        我的问题是因为我添加了第二个参数:

                        AngularFireModule.initializeApp(firebaseConfig, 'reservas')
                        

                        如果我删除第二个参数它工作正常:

                        AngularFireModule.initializeApp(firebaseConfig)
                        

                        【讨论】:

                        • 如果你使用 firebase.initializeApp 而不是 AngularFireModule.initializeApp 是否重要
                        • 第二个参数创建一个新的“app”句柄。如果省略该参数,则暗示应用名称[DEFAULT],因此是“默认应用”。为什么这对您很重要,例如代码可以隐式使用默认值。 firebase.firestore() 而对于自定义“应用程序”(首先)返回值,必须传递一个“应用程序句柄”。我用两种方式。主 Web 应用程序和库中命名变体的默认值。
                        【解决方案17】:

                        你在 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)
                                })
                            }
                        

                        【讨论】:

                        • 我认为这与所述问题无关。
                        【解决方案18】:

                        这可能不是最佳答案,但我必须使用管理员和 firebase 初始化应用程序,如下所示。我将 admin 用于自己的目的和 firebase。

                        const firebase = require("firebase");
                        const admin = require("firebase-admin");
                        
                        admin.initializeApp(functions.config().firebase);
                        firebase.initializeApp(functions.config().firebase);
                        // Get the Auth service for the default app
                        var authService = firebase.auth();
                        
                         function createUserWithEmailAndPassword(request, response) {
                                const email = request.query.email;
                                const password = request.query.password;
                                if (!email) {
                                    response.send("query.email is required.");
                                    return;
                                }
                                if (!password) {
                                    response.send("query.password is required.");
                                    return;
                                }
                                return authService.createUserWithEmailAndPassword(email, password)
                                    .then(success => {
                                        let responseJson = JSON.stringify(success);
                                        console.log("createUserWithEmailAndPassword.responseJson", responseJson);
                                        response.send(responseJson);
                                    })
                                    .catch(error => {
                                        let errorJson = JSON.stringify(error);
                                        console.log("createUserWithEmailAndPassword.errorJson", errorJson);
                                        response.send(errorJson);
                                    });
                            }
                        

                        【讨论】:

                          【解决方案19】:

                          我在找到 here 的 Firebase 在线指南后遇到了类似的问题。

                          “初始化多个应用程序”部分的标题具有误导性,因为该标题下的第一个示例实际上演示了如何初始化单个默认应用程序。这是说的例子:

                          // Initialize the default app
                          var defaultApp = admin.initializeApp(defaultAppConfig);
                          
                          console.log(defaultApp.name);  // "[DEFAULT]"
                          
                          // Retrieve services via the defaultApp variable...
                          var defaultAuth = defaultApp.auth();
                          var defaultDatabase = defaultApp.database();
                          
                          // ... or use the equivalent shorthand notation
                          defaultAuth = admin.auth();
                          defaultDatabase = admin.database();
                          

                          如果您从之前的 2.x SDK 迁移,您将必须更新访问数据库的方式,如上所示,否则您将收到 No Firebase App '[DEFAULT]' 错误。

                          Google 在以下方面有更好的文档:

                          1. 初始化:https://firebase.google.com/docs/database/admin/start

                          2. 保存:https://firebase.google.com/docs/database/admin/save-data

                          3. 检索: https://firebase.google.com/docs/database/admin/retrieve-data

                          【讨论】:

                            猜你喜欢
                            • 2018-01-13
                            • 2020-11-19
                            • 1970-01-01
                            • 2022-11-07
                            • 2023-01-24
                            • 2021-07-13
                            • 2021-06-12
                            • 2021-07-25
                            • 1970-01-01
                            相关资源
                            最近更新 更多