0

創建使用角2 CLI火力地堡應用程序,我有以下代碼:否「[默認]」已經在角2

joinSubmit(){ 
    var self = this; 
    firebase.auth().createUserWithEmailAndPassword(
     '[email protected]', 
     'testpassword' 
     ) 
     .then(function(response){ 
      var userdetail = {}; 
      userdetail['/users/'+response.uid] = { 
       username: 'test', 
       useremail: '[email protected]' 
      }; 
      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" 
    } 
} 

不要知道如何解決這個錯誤。請指教。謝謝。

UPDATE因此docs有一個腳本來初始化Firebase應用程序。

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

我該如何將此功能合併到我的Firebase功能中?就像,比如說,如果我有下面的代碼:

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

UPDATE

所以來找似乎有某種問題,讓我app.module初始化我的火力地堡設置。作爲具有這樣的功能做的伎倆:

rest(){ 

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

    firebase.initializeApp(); 
     firebase.auth().createUserWithEmailAndPassword('[email protected]', 'hispassword'); 
    } 

雖然這個工作,它似乎也難以理想必須鍵入firebase.initializeApp()我每次撥打電話時。

+1

你初始化應用程序? –

+0

如果我把'firebase.initializeApp(firebaseConfig);'放在我的'firebaseConfig'下面,它仍然不會做任何事情。 – abrahamlinkedin

+0

K.我在看文檔,並有一個關於它的問題。將更新此q。 – abrahamlinkedin

回答

1

我想這會幫助你。在主要組件中初始化應用程序。

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

檢查以下內容plunker

+0

所以在我最初的問題中,我忘記了包括我有'\t AngularFireModule.initializeApp(firebaseConfig)'。我完全不好。我已經更新了q。我曾嘗試像你一樣做,而這並沒有改變任何東西。我懷疑,正如文檔所說,我需要使用初始化函數。 – abrahamlinkedin