从Firebase数据库中获取数据时出错

时间:2019-02-16 15:32:53

标签: angular firebase

我正在尝试从Firebase数据库中获取数据,但是在控制台中出现以下错误

  

Firestore(5.8.3):无法访问Cloud Firestore后端。   连接失败1次。最新错误:FirebaseError:   [code = failed-precondition]:Cloud Firestore API不可用   用于Cloud Datastore项目。

服务文件

import { Injectable } from '@angular/core';
  import { AngularFirestore } from '@angular/fire/firestore';

@Injectable({
  providedIn: 'root'
})
export class CategoryService {

constructor(private firestore: AngularFirestore) { }

getcategories(){
return this.firestore.collection('categories').snapshotChanges();
}
}

.ts文件

import { Component, OnInit } from '@angular/core';
import { CategoryService } from 'src/app/category.service';

@Component({
selector: 'app-newproducts',
templateUrl: './newproducts.component.html',
styleUrls: ['./newproducts.component.scss']
})
export class NewproductsComponent implements OnInit {

categories

constructor( ctservice:CategoryService) { 
this.categories=ctservice.getcategories()

 }

ngOnInit() {
}

}

html文件

<div class="md-form form-group mt-5">
<select class="browser-default custom-select">
<option value=""></option>
<option *ngFor="let c of categories | async " [value]="c.$key">
{{c.name}}
</option>

  </select>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { MDBBootstrapModule } from 'angular-bootstrap-md';
import { AppComponent } from './app.component';

import { Approuting } from './app-routing.module';
import { HeaderComponent } from './header/header.component';
import { ShoppingCartComponent } from './shopping-cart/shopping- 
cart.component';
import { LoginComponent } from './login/login.component';
import { UsernameComponent } from './username/username.component';
import { MyordersComponent } from 
'./username/myorders/myorders.component';
import { AdminordersComponent } from 
'./username/adminorders/adminorders.component';
import { AdminproductsComponent } from 
'./username/adminproducts/adminproducts.component';
import { LogoutComponent } from 
'./username/logout/logout.component';
import { HttpModule } from '@angular/http';
import { NewproductsComponent } from 
'./username/adminproducts/newproducts/newproducts.component';

import { CategoryService } from './category.service';
import { AngularFireModule } from '@angular/fire';
import { AngularFireDatabaseModule } from '@angular/fire/database';
import { environment } from '../environments/environment';
import { AngularFirestore,FirestoreSettingsToken } from 
'@angular/fire/firestore';

@NgModule({
declarations: [
AppComponent,
HeaderComponent,
ShoppingCartComponent,
LoginComponent,
UsernameComponent,
MyordersComponent,
AdminordersComponent,
AdminproductsComponent,
LogoutComponent,
NewproductsComponent,

],
imports: [
BrowserModule,
MDBBootstrapModule.forRoot(),
Approuting,
HttpModule,
AngularFireModule.initializeApp(environment.firebaseConfig),
AngularFireDatabaseModule
],
providers: [CategoryService,AngularFirestore,
{ provide: FirestoreSettingsToken, useValue: {} }
],
 bootstrap: [AppComponent]
})
export class AppModule { }

1 个答案:

答案 0 :(得分:1)

当您引用的Firebase项目没有激活Firestore时,会发生此错误。

In this example未启用Firestore引用的项目。启用它后,错误消失了。

如果不熟悉任何人,请按照以下步骤为每个项目启用Cloud Firestore。

  • 登录项目在the Firebase site上处于活动状态的帐户
  • 转到the console link
  • 选择您要访问的项目(该项目中的firebase配置用于该项目)
  • 在“开发”部分下方的左侧,选择“数据库”链接
  • 单击Cloud Firestore的“创建数据库”按钮