输入'Observable&lt; {} []&gt;'无法转换为'AngularFireList <any []>'

时间:2018-06-01 15:50:24

标签: javascript angular firebase firebase-realtime-database observable

我对Firebase非常陌生,我正在学习这门课程:https://scotch.io/tutorials/build-a-status-update-app-w-reactions-using-angular-v4-and-firebase但我的问题是本教程已过时,因此Firebase遇到了一些问题

在教程中,有一个最近的函数用于获取最新的帖子...之前代码看起来像这样:

// ----------------------------------------------------------------------
// Method to get the recent statuses from Firebase
// ----------------------------------------------------------------------

recent(amount: number): FirebaseListObservable<any[]> {
  return this.statuses = this.af.list('/statuses').map(arr => arr.reverse()) as FirebaseListObservable<any[]>;
}

我后来发现FirebaseListObservable已过时,现在是AngularFireList

所以我将代码改为看起来像这样

recent(amount: number): AngularFireList<any[]> {
  return this.statuses = this.af.list('/statuses').map(arr => arr.reverse()) as AngularFireList<any[]>;
}

我在Property 'map' does not exist on type 'AngularFireList<{}>'中使用了类似的问题,并尝试将我的代码更改为:

recent(amount: number): AngularFireList<any[]> {
  return this.statuses = this.af.list('/statuses').valueChanges().map(arr => arr.reverse()) as AngularFireList<any[]>;
}

Type 'Observable<{}[]>' cannot be converted to type 'AngularFireList<any[]>'.

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可能希望将map()用作RxJS 5.5+的可管理运算符。这将允许您反转从.list()返回的数组。然后,您应该只需将其键入Observable<YourMagicType[]>

即可
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

recent(amount: number): Observable<YourMagicType[]> {
  return this.af.list<YourMagicType>('/statuses').valueChanges().pipe(
    map(arr => arr.reverse())
  );
}

希望这有帮助!