类型“ Observable <Admin []>”必须具有返回迭代器的“ [Symbol.iterator]()”方法

时间:2019-11-23 05:50:12

标签: angular google-cloud-firestore angularfire

这是我的管理界面

export interface Admin{
  name: string;
  email: string;
  telephone: string;
  address: string;
  nic: string;
}

这是我的构造函数

  constructor(
    private afs: AngularFirestore,
    private admins: Observable<Admin[]>,
    private adminDoc: AngularFirestoreCollection<Admin>
    ){
      this.adminDoc = this.afs.collection<Admin>('users/user/admin');  
      this.admins = this.adminDoc.snapshotChanges().pipe(
        map(actions => actions.map(a=>{
          var data = a.payload.doc.data() as Admin;
          const id = a.payload.doc.id;      
          return {id,...data}
        }))
      );
     }

这是我得到错误的地方

for(let admin of this.admins){
}

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

this.admins现在是一个可观察的对象,您不能迭代一个可观察的对象,您将必须订阅它。或在内部移动for循环逻辑:

  map(actions => actions.map(a=>{
       var data = a.payload.doc.data() as Admin;
       const id = a.payload.doc.id; 

        //====> you have access to all items of here

        return {id,...data}
   }))

或者像这样订阅它:

this.admins.subscribe(admins => console.log(admins));