AngularFire Firestore 属性“地图”在类型“Action<DocumentSnapshot<any>>”上不存在

wpe*_*per 3 firebase angularfire2 angular google-cloud-firestore

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

我的 IDE 显示以下错误:

类型“Action< DocumentSnapshot< any>>”上不存在属性“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 };
    })
  })
)
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,出现以下错误:

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

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

"firebase": "^5.0.4"
"@angular/core": "^6.1.0"
"angularfire2": "^5.0.0-rc.10"
Run Code Online (Sandbox Code Playgroud)

Tim*_*ens 5

您的映射代码错误:

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 };
    })
 })
Run Code Online (Sandbox Code Playgroud)

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

尝试:

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