【问题标题】:AngularFire Firestore Property 'map' does not exist on type 'Action<DocumentSnapshot<any>>''Action<DocumentSnapshot<any>>' 类型上不存在 AngularFire Firestore 属性“地图”
【发布时间】:2019-01-21 09:01:51
【问题描述】:

我正在关注集合上的 firebase docs,但在尝试检索具有文档 ID 的文档集合时,我无法弄清楚为什么我的代码无法按照文档工作。

我的 IDE 显示以下错误:

“Action>”类型上不存在属性“map”。

对于以下代码:

this.userDocRef = this.afs.doc<any>(`users/${user.uid}`);
this.userDoc$ = this.userDocRef.snapshotChanges().pipe(
  map(actions => {
    return actions.map(a => { // error throws here
      const data = a.payload.doc.data() as any;
      const id = a.payload.doc.id;
      return { id, ...data };
    })
  })
)

当我运行此代码时,我收到以下错误:

错误类型错误:actions.map 不是函数

如何重构它以使其正常工作?

"firebase": "^5.0.4"
"@angular/core": "^6.1.0"
"angularfire2": "^5.0.0-rc.10"

【问题讨论】:

  • 从'rxjs/operators'导入{地图};尝试使用 .pipe(map( a => ...
  • 我正在从 rxjs 导入地图,而 actions 不是可观察的,所以这不起作用

标签: angular firebase google-cloud-firestore angularfire2


【解决方案1】:

您的映射代码错误:

map(actions => {
    return actions.map(a => { // error throws here
      const data = a.payload.doc.data() as any;
      const id = a.payload.doc.id;
      return { id, ...data };
    })
 })

这用于映射集合。您不是在迭代一个集合,而是一个文档

试试:

this.userDoc$ = this.userDocRef.snapshotChanges()
    .pipe(
        map(action => {
            const data = action.payload.data();
            const id = action.payload.id;
            return { id, ...data };
        })
     );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-26
    • 2020-10-08
    • 2021-02-08
    • 2020-01-11
    • 2021-01-19
    • 2019-09-20
    • 2018-09-09
    • 2019-12-31
    相关资源
    最近更新 更多