TypeError:changes.payload.docChanges不是使用AngularFire2 v5-rc9的Angular 6中的函数

时间:2018-05-17 08:57:12

标签: angular firebase google-cloud-firestore angularfire2

我最近收到此错误(" angularfire2":" ^ 5.0.0-rc.9")。我的代码与this example中的代码相同 我可以看到其他开发人员on github也发生了这个错误,但没有找到任何解决方案。我使用的是Angular 6和最新版本的chrome。

import { Component } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Shirt { name: string; price: number; }
export interface ShirtId extends Shirt { id: string; }

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let shirt of shirts | async">
        {{ shirt.name }} is {{ shirt.price }}
      </li>
    </ul>
  `
})
export class AppComponent {
  private shirtCollection: AngularFirestoreCollection<Shirt>;
  shirts: Observable<ShirtId[]>;
  constructor(private readonly afs: AngularFirestore) {
    this.shirtCollection = afs.collection<Shirt>('shirts');
    // .snapshotChanges() returns a DocumentChangeAction[], which contains
    // a lot of information about "what happened" with each change. If you want to
    // get the data and the id use the map operator.
    this.shirts = this.shirtCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as Shirt;
        const id = a.payload.doc.id;
        return { id, ...data };
      }))
    );
  }
}

2 个答案:

答案 0 :(得分:3)

最新版本的AngularFire需要Firebase 5.0.2+,他们将docChanges从属性更改为函数。确保您已@firebase/firestore升级@firebase/firestore-typespackage.json

答案 1 :(得分:-1)

Angular 6引入了RXJS 6,改变了RXJS的使用方式。迁移链接here。但是Angularfire2还没有更新到RXJS。等待他们更新。与此同时,您可以使用rxjs-compact并具有向后兼容性

npm install rxjs-compat --save