【问题标题】:Adding firestore to Angular typescrit error 2448将 Firestore 添加到 Angular 打字稿错误 2448
【发布时间】: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


【解决方案1】:

尝试添加这个:

import * as firebase from 'firebase/app';

import 'firebase/firestore';

另请检查:

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

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

How to create (set) data to Firestore using AngularFire?

【讨论】:

    猜你喜欢
    • 2018-11-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多