Angular 6 firebase 从云 firestore 获取列表

Angular 6 firebase get list from cloud firestore

我想知道如何从 Cloud Firestore 获取列表。

我这样上传列表:

export interface Data {
  name: string;
  address: string;
  address2: string;
  pscode: string;
  ccode: string;
  name2: string;
}

constructor(private afs: AngularFirestore){
    this.notesCollection = this.afs.collection(`imones`, (ref) => ref.orderBy('time', 'desc').limit(5));
}

  notesCollection: AngularFirestoreCollection<Data>;

//creating item in list (imones)

createItem(){
  this.notesCollection.add(this.busines);
}

现在的问题是如何从那里获取所有列表项?

这是我的尝试:

constructor(private afs: AngularFirestore){
  this.items = this.notesCollection.valueChanges();
}
  items: Observable<Data[]>;

HTML :

 <p *ngFor="let item of items">{{item.name}}</p>

错误:

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

再次出错:

您正在尝试迭代一个可观察对象。 *ngFor 循环允许您仅迭代可迭代对象(数组、集合)

您有 2 个选择:

  1. 使用内置的 async pipe 来迭代一个可观察对象。 例如:

    <p *ngFor="let item of items | async">{{item.name}}</p>

  2. 订阅 observable 并用它的最新结果更新一个变量(你的情况下的 observable 结果是一个数组):

    this.notesCollection.valueChanges()
                        .subscribe((items) => this.items = items);