将firestore添加到Angular typescrit错误2448



我正在将Firestore添加到现有的angular项目中,但在尝试使用文档代码向构造函数进行编辑时出现TS错误:

import { Firestore, collectionData, collection } from '@angular/fire/firestore';
constructor(db: Firestore) {
const collection: any = collection(db, 'songs');
this.songs$ = collectionData(collection);
}
TS error: Block-scoped variable 'collection' used before its declaration.ts(2448)
Type 'Observable<DocumentData[]>' is missing the following properties from type '{ new (subscribe?: ((this: Observable<Song[]>, subscriber: Subscriber<Song[]>) => TeardownLogic) | undefined): Observable<Song[]>; prototype: Observable<...>; create: (...args: any[]) => any; }': prototype, create ts(2739)

消防仓库版本:@"角/火":"7.4.1";而ts版本:typescript":"4.7.2";

我不明白如何绕过这个问题,也没能在这个具体问题上找到任何答案。

collection变量重命名为其他变量;

import { Firestore, collectionData, collection } from '@angular/fire/firestore';
constructor(db: Firestore) {
const coll: any = collection(db, 'songs');
this.songs$ = collectionData(coll);
}

尝试添加以下内容:

从"firebase/app"导入*作为firebase;

导入"firebase/firestore";

同时检查:

https://firebase.google.com/docs/firestore/manage-data/add-data

以下是几乎相关的其他链接解决方案:

如何使用AngularFire创建(设置(Firestore数据?

最新更新