在Angular 2中没有创建Firebase应用程序'[DEFAULT]'

时间:2016-10-04 17:01:24

标签: angular firebase firebase-realtime-database firebase-authentication

使用Angular 2 CLI,我有以下代码:

joinSubmit(){
    var self = this;
    firebase.auth().createUserWithEmailAndPassword(
        'test@test.com', 
        'testpassword'
        )
        .then(function(response){
            var userdetail = {};
            userdetail['/users/'+response.uid] = {
                username: 'test',
                useremail: 'test@test.com'
            };
            firebase.database().ref().update(userdetail);
        });
        this.router.navigate(['profile']);
}

不幸的是,当我运行它时,我收到No Firebase App [DEFAULT] has been created - call Firebase App.initializeApp()错误。

我的 app.module 看起来如此(显然填写了API密钥):

import * as firebase from 'firebase';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AngularFireModule } from 'angularfire2';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { J2InvestRoutingModule } from './app-routing.module';
import { NavigationComponent } from './navigation/navigation.component';
import { JoinComponent } from './join/join.component';
import { SignComponent } from './sign/sign.component';
import { ProfileComponent } from './profile/profile.component';


  export const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    storageBucket: "",
    messagingSenderId: ""
  };

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    NavigationComponent,
    JoinComponent,
    SignComponent,
    ProfileComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    J2InvestRoutingModule,
    ReactiveFormsModule,
    AngularFireModule.initializeApp(firebaseConfig)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

同时我的 package.json 如下所示:

{
  "name": "j2-invest",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "angularfire2": "^2.0.0-beta.5",
    "core-js": "^2.4.1",
    "firebase": "^3.4.1",
    "rxjs": "5.0.0-beta.12",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "@types/jasmine": "^2.2.30",
    "angular-cli": "1.0.0-beta.16",
    "codelyzer": "~0.0.26",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "3.13.0",
    "typescript": "2.0.2"
  }
}

不知道如何解决此错误。请指教。感谢。

更新因此docs有一个脚本可用于初始化Firebase应用。

  // Intialize the "[DEFAULT]" App
  var mainApp = firebase.initializeApp({
    // ...
  }); 

如何将此功能合并到我的Firebase功能中?比方说,如果我有以下代码:

runner(){
    var mainApp = firebase.initializeApp({});
    firebase.auth().createUserWithEmailAndPassword('chase', 'myface');    
}

更新

所以来发现我的app.module初始化我的Firebase设置似乎存在某种问题。由于具有这样的功能可以解决问题:

    rest(){

    const firebaseConfig = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    storageBucket: "",
    messagingSenderId: ""
};

    firebase.initializeApp();
        firebase.auth().createUserWithEmailAndPassword('chevy@chase.com', 'hispassword'); 
    } 

虽然这有效,但每次拨打电话时都必须输入firebase.initializeApp()似乎并不理想。

1 个答案:

答案 0 :(得分:1)

我认为这会对你有所帮助。在主要组件中初始化应用程序。

AngularFireModule.initializeApp({
  apiKey: "*******************",
  authDomain: "**********.firebaseapp.com",
  databaseURL: "https://*********.firebaseio.com",
  storageBucket: ""
})

检查以下plunker