无法将类型 'Observable<{}[]>' 转换为类型 'AngularFireList<any[]>'



我几乎是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[]>;
}

我使用了"AngularFireList<{}>"类型上不存在的属性"map"中的类似问题,并尝试将我的代码更改为:

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[]>'.

谁能帮我?

您可能希望将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())
);
}

希望对您有所帮助!

相关内容

最新更新