首页 > 解决方案 > 尝试从 Firebase Firestore 获取集合时,“类型 '{}' 上不存在属性 'map'”

问题描述

我已按照此处的文档从 firestore 获取带有文档 ID 的数据。

  import { Injectable } from '@angular/core';
  import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
  import { Observable } from 'rxjs';
  import { map } from 'rxjs/operators';
  import 'rxjs/add/operator/map';

  @Injectable()
  export class UserService {
  userCol : AngularFirestoreCollection<UserInter>;
  users : Observable<UserInter[]>;
  constructor(private afs:AngularFirestore) { } 

  GetUsers(){
    this.userCol = this.afs.collection<UserInter>('users');
    this.users = this.userCol.snapshotChanges().pipe(
                map(changes =>{
 error here->   return changes.map(a => {
                const data = a.payload.doc.data() as UserInter;
                data.id = a.payload.doc.id;
                return data;
          })
        }))
return this.users;  
}

export interface UserInter {
email ?: string,
Firstname ?: string,
Lastname ?: string,
Address ?: string,
}

export interface UserInterid extends UserInter {id ?: string }

当我 ng-serve 我的应用程序时,我收到此错误

类型“{}”上不存在属性“地图”

标签: angularfirebasegoogle-cloud-firestoreangularfire2rxjs6

解决方案


GetUsers(){
    this.userCol = this.afs.collection<UserInter>('users');
    this.users = this.userCol.stateChanges(['added']).pipe(
                map(changes =>{
             return changes.map(a => {
                const data = a.payload.doc.data() as UserInter;
                data.id = a.payload.doc.id;
                return data;
          })
        }))
return this.users;  
}

推荐阅读