【发布时间】:2022-11-11 12:09:55
【问题描述】:
我正在将 Firestore 添加到现有的 Angular 项目中,但是在尝试使用文档代码将 dit 添加到构造函数时出现 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)
Firestore 版本:“@angular/fire”:“^7.4.1” 以及 ts 版本: typescript": "~4.7.2"
我不明白如何解决这个问题,也无法就这个特定问题找到任何答案。
【问题讨论】:
-
我有一种感觉 Typescript 认为集合(声明的 const)与集合(方法)相同。如果您将集合命名为其他任何名称(例如 const myCollection),问题是否仍然存在?
-
谢谢,您的评论确实解决了收藏问题,但第二个 ts 错误仍然存在
标签: angular typescript firebase google-cloud-firestore