gpt4 book ai didi

angular - 导入 AngularFirestoreModule : "This type parameter might need an ` extends firebase. firestore.DocumentData` 约束时出现问题。”

转载 作者:行者123 更新时间:2023-12-05 04:19:16 26 4
gpt4 key购买 nike

您好,我在将 Firebase (Firestore) 连接到我的 Angular 应用程序时遇到问题。那是我第一次尝试连接 firebase,所以我遵循了 Google 的教程。 https://developers.google.com/codelabs/building-a-web-app-with-angular-and-firebase#10

在第 11 步中,我必须导入 AngularFirestoreModule,我这样做了:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { MapComponent } from './map/map.component';
import {BrowserAnimationsModule} from "@angular/platform-browser/animations";
import { NavbarComponent } from './navbar/navbar.component';
import { CodeComponent } from './navbar/code/code.component';
import { ManagerComponent } from './manager/manager.component';
import { initializeApp,provideFirebaseApp } from '@angular/fire/app';
import { environment } from '../environments/environment';
import { provideFirestore,getFirestore } from '@angular/fire/firestore';
import {AngularFireModule} from "@angular/fire/compat";
import {AngularFirestoreModule} from "@angular/fire/compat/firestore";

@NgModule({
declarations: [
AppComponent,
MapComponent,
NavbarComponent,
CodeComponent,
ManagerComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
provideFirebaseApp(() => initializeApp(environment.firebase)),
provideFirestore(() => getFirestore()),
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule
],
providers: [
ManagerComponent,
CodeComponent
],
bootstrap: [AppComponent]
})
export class AppModule { }

导入后我遇到了以下问题:

Error: node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:18 - error TS2430: Interface 'DocumentSnapshotExists<T>' incorrectly extends interface 'DocumentSnapshot<DocumentData>'.
The types returned by 'data(...)' are incompatible between these types.
Type 'T' is not assignable to type 'DocumentData | undefined'.
Type 'T' is not assignable to type 'DocumentData'.

13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
~~~~~~~~~~~~~~~~~~~~~~

node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
~
This type parameter might need an `extends firebase.firestore.DocumentData` constraint.
node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
~
This type parameter might need an `extends firebase.firestore.DocumentData | undefined` constraint.


Error: node_modules/@angular/fire/compat/firestore/interfaces.d.ts:23:18 - error TS2430: Interface 'QueryDocumentSnapshot<T>' incorrectly extends interface 'QueryDocumentSnapshot<DocumentData>'.
The types returned by 'data(...)' are incompatible between these types.
Type 'T' is not assignable to type 'DocumentData'.

node_modules/@angular/fire/compat/firestore/interfaces.d.ts:29:33
29 export interface DocumentChange<T> extends firebase.firestore.DocumentChange {
~
This type parameter might need an `extends firebase.firestore.DocumentData` constraint.




× Failed to compile.

我不知道如何解决我的问题或如何以其他方式连接我的 Firestore 数据库。感谢您的帮助!

这也是我的 app.component.ts:

import { Component } from '@angular/core';
import {AngularFirestore} from "@angular/fire/compat/firestore";
import {Observable} from "rxjs";

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
toMap:boolean = true;
items: Observable<any[]>;

constructor(firestore: AngularFirestore) {
this.items = firestore.collection('items').valueChanges();
}

switchToMap(){
this.toMap = !this.toMap;
}
}

我尝试按照 Google 的教程进行操作,但遇到了问题,该问题已在上方字段中进行了描述。 https://developers.google.com/codelabs/building-a-web-app-with-angular-and-firebase#10

最佳答案

我遇到了同样的问题,这条 Github 评论帮我解决了这个问题: https://github.com/angular/angularfire/issues/3290#issuecomment-1323837275

在你的node_modules/@angular/fire/compat/firestore/interfaces.d.ts , 添加一个 generic <T>到错误所在的接口(interface)的末尾。

首先,从这里更新这一行:

export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot 

为此:

export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot<T> 

对所有人都这样做。

enter image description here

关于angular - 导入 AngularFirestoreModule : "This type parameter might need an ` extends firebase. firestore.DocumentData` 约束时出现问题。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74868274/

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