简体   繁体   English

如何使用angular 6配置Firebase?

[英]How to configure firebase with angular 6?

I am beginner of angular 6. I am trying to configure angular 6 with firebase, but at the time of set data i am getting below error. 我是angular 6的初学者。我正在尝试使用firebase配置angular 6,但是在设置数据时,我的状态低于错误。

FirebaseError {code: "app/no-app", message: "Firebase: No Firebase App '[DEFAULT]' has been cre…- call Firebase App.initializeApp() (app/no-app).", name: "[DEFAULT]", ngDebugContext: DebugContext_, ngErrorLogger: ƒ, …} code: "app/no-app" message: "Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app)." FirebaseError {代码:“ app / no-app”,消息:“ Firebase:没有Firebase应用程序'[DEFAULT]'已创建...-调用Firebase App.initializeApp()(app / no-app)。”,名称:“ [DEFAULT]”,ngDebugContext:DebugContext_,ngErrorLogger:ƒ,…}代码:“ app / no-app”消息:“ Firebase:未创建Firebase应用程序'[DEFAULT]'-调用Firebase App.initializeApp()(app /不应用)。” name: "[DEFAULT]" ngDebugContext: DebugContext_ {view: {…}, nodeIndex: 76, nodeDef: {…}, elDef: {…}, elView: {…}} ngErrorLogger: ƒ () stack: "[DEFAULT]: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).↵ at error ( http://localhost:4200/vendor.js:73214:21 )↵ at app ( http://localhost:4200/vendor.js:73097:13 )↵ at Object.serviceNamespace [as firestore] ( http://localhost:4200/vendor.js:73155:47 )↵ at RegiComponent.push../src/app/regi/regi.component.ts.RegiComponent.saveData ( http://localhost:4200/main.js:315:68 )↵ at Object.eval [as handleEvent] (ng:///AppModule/RegiComponent.ngfactory.js:214:27)↵ at handleEvent ( http://localhost:4200/vendor.js:55060:41 )↵ at callWithDebugContext ( http://localhost:4200/vendor.js:56154:25 )↵ at Object.debugHandleEvent [as handleEvent] ( http://localhost:4200/vendor.js:55857:12 )↵ at dispatchEvent ( http://localhost:4200/vendor.js:52509:25 )↵ at http://localhost:4200/ve 名称:“ [[DEFAULT]”” ngDebugContext:DebugContext_ {view:{…},nodeIndex:76,nodeDef:{…},elDef:{…},elView:{…}} ngErrorLogger:ƒ()堆栈:“ [DEFAULT] :Firebase:未创建Firebase应用程序'[DEFAULT]'-调用Firebase App.initializeApp()(app / no-app).↵错误( http:// localhost:4200 / vendor.js:73214:21 ) app在应用程序上( http:// localhost:4200 / vendor.js:73097:13 )↵在RegiComponent上的Object.serviceNamespace [作为Firestore ]( http:// localhost:4200 / vendor.js:73155:47 )↵在Object.eval上推送../src/app/regi/regi.component.ts.RegiComponent.saveData(http:// localhost:4200 / main.js:315:68 )↵[作为handleEvent](ng:// /AppModule/RegiComponent.ngfactory.js:214:27)↵在handleEvent( http:// localhost:4200 / vendor.js:55060:41 )↵在callWithDebugContext( http:// localhost:4200 / vendor.js:56154 :25 )↵在Object.debugHandleEvent [作为handleEvent]( http:// localhost:4200 / vendor.js:55857:12 )↵在dispatchEvent( http:// localhost:4200 / vendor.js:52509:25 )↵在http:// localhost:4200 / ve ndor.js:52956:38 " proto : Error. ndor.js:52956:38proto :错误。 Please have look of my app.module.ts 请看看我的app.module.ts

 import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { AngularFireModule } from '@angular/fire'; import { AngularFirestoreModule } from '@angular/fire/firestore'; import { environment } from '../environments/environment'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { RegiComponent } from './regi/regi.component'; import { LoginComponent } from './login/login.component'; @NgModule({ declarations: [ AppComponent, RegiComponent, LoginComponent ], imports: [ BrowserModule, FormsModule, AngularFireModule.initializeApp(environment.firebaseConfig), AngularFirestoreModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } 

This is my environment.ts 这是我的环境

 export const environment = { production: false, firebaseConfig: { apiKey: "AIzaSyBNTXxgG5hkzY3_E6E3VRwlMQ798wqBjXA", authDomain: "firsapp-f3cf4.firebaseapp.com", databaseURL: "https://firsapp-f3cf4.firebaseio.com", projectId: "firsapp-f3cf4", storageBucket: "firsapp-f3cf4.appspot.com", messagingSenderId: "736958488941" } //firebase.initializeApp(firebaseConfig); }; 

Below is my function i am calling on click 以下是我在点击时要调用的函数

 saveData($event){ let name : string; let email : string; let passWord : string; let city : string[]; //var database = firebase.database(); // Add a new document in collection "cities" let db = firebase.firestore(); // Add a new document in collection "cities" db.collection("cities").doc("LA").set({ city : ['A','B', 'C', 'D'], }) .then(function() { console.log("Document successfully written!"); }) .catch(function(error) { console.error("Error writing document: ", error); }); 

Please help me if you need any further info please know me. 如果您需要任何进一步的信息,请帮助我,请了解我。

Try the following using angularfire2 service AngularFirestore instead of directly trying to access 'firebase`. 使用angularfire2服务AngularFirestore尝试以下操作,而不是直接尝试访问“ firebase ”。 You can stream data from the collection using either valueChanges() or snapshotChanges() and utilize RxJS operators to additional work with the data as needed. 您可以使用valueChanges()snapshotChanges()从集合中流式传输数据,并根据需要利用RxJS运算符对数据进行其他处理。

import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

@Component({ /* ... */})
export class SomeComponent implements OnInit {
  // Should ideally use strong types instead of 'any' for better IDE support
  private itemsCollection: AngularFirestoreCollection<any>;
  items: Observable<any[]>;

  constructor(private afs: AngularFirestore) {
    this.itemsCollection = afs.collection<any>('cities');
    this.items = this.itemsCollection.valueChanges();
  }

  ngOnInit() {
    // use RxJS operators to use the data as needed
    this.items.subscribe(cities => console.log(cities));
  }

  saveData() {
    this.itemsCollection.doc('LA').set({ city:  ['A','B', 'C', 'D']} );
  }
}

I'd highly recommend to review all the angularfire2 Cloud Firestore docs before continuing as well. 我强烈建议您先阅读所有angularfire2 Cloud Firestore 文档,然后再继续。

Hopefully that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM