我对 Firebase 还很陌生,我正在学习这门类(class):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[]>'.
谁能帮帮我吗?
最佳答案
您可能想要使用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())
);
}
希望有帮助!
关于javascript - 类型 'Observable<{}[]>' 无法转换为类型 'AngularFireList<any[]>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647112/