gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:11:56 25 4
gpt4 key购买 nike

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

Firestore (5.8.3): Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: FirebaseError: [code=failed-precondition]: The Cloud Firestore API is not available for Cloud Datastore projects.



服务文件
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 { }

最佳答案

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

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

如果有人不熟悉,以下是为每个项目启用 Cloud Firestore 的步骤。

  • 登录项目在 the Firebase site 上处于事件状态的帐户
  • 转至 the console link
  • 选择您尝试访问的项目(项目中的 firebase 配置适用于)
  • 在左侧的“开发”部分下,选择“数据库”链接
  • 单击 Cloud Firestore 的“创建数据库”按钮
  • 关于angular - 从 Firebase 数据库获取数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54724684/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com