简体   繁体   English

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

[英]Error while fetching data from firebase database

I am trying to fetch my data from the firebase database but i am getting the following error in my console 我正在尝试从Firebase数据库中获取数据,但是在控制台中出现以下错误

Firestore (5.8.3): Could not reach Cloud Firestore backend. Firestore(5.8.3):无法访问Cloud Firestore后端。 Connection failed 1 times. 连接失败1次。 Most recent error: FirebaseError: [code=failed-precondition]: The Cloud Firestore API is not available for Cloud Datastore projects. 最新错误:FirebaseError:[code = failed-precondition]:Cloud Firestore API不适用于Cloud Datastore项目。

service file 服务档案

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 file .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 file 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 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 { }

This error occurs when you are referencing a Firebase project that does not have Firestore active. 当您引用的Firebase项目没有激活Firestore时,会发生此错误。

In this example Firestore was not enabled the the project being referenced. 在此示例中 ,未启用Firestore引用的项目。 After enabling it, the error was gone. 启用它后,错误消失了。

In case anyone is unfamiliar, here are the steps to enable the Cloud Firestore per project. 如果有人不熟悉,请按照以下步骤为每个项目启用Cloud Firestore。

  • Log into the account the project is active in on the Firebase site 登录到该项目在Firebase站点上处于活动状态的帐户
  • Go to the console link 转到控制台链接
  • Select the project you are trying to access (that the firebase config in the project is for) 选择您要访问的项目(该项目中的firebase配置用于该项目)
  • On the left side under the "Development" section, select the "Database" link 在“开发”部分下方的左侧,选择“数据库”链接
  • Click the "Create Database" button for Cloud Firestore 单击Cloud Firestore的“创建数据库”按钮

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

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