【发布时间】:2020-01-10 14:11:02
【问题描述】:
我有一个 Angular 8 应用程序并使用 firebase。我想从服务中检索数据。所以我做了一个检索数据的函数。但是我在该功能上遇到错误
我搜索了很多并阅读了 firebase 文档。但它并没有解决问题。
这就是我拥有的功能:
findCourseByUrl(courseUrl: string): Observable<Course> {
return this.db.list('courses', ref => {
return ref.orderByChild('url').equalTo(courseUrl);
}).snapshotChanges().pipe(map(data => data[0]));
}
但我仍然收到此错误:
Type 'Observable<AngularFireAction<DatabaseSnapshot<unknown>>>' is not assignable to type 'Observable<Course>'.
Type 'AngularFireAction<DatabaseSnapshot<unknown>>' is missing the following properties from type 'Course': id, url
如果我这样做:
findCourseByUrl(courseUrl: string): Observable<Course> {
return this.db.list('courses', ref => {
return ref.orderByChild('url').equalTo(courseUrl);
}).snapshotChanges().pipe(
map(actions => {
return actions.map(a => {
const data = a.payload.doc.data() as Course;
return { ...data };
});
}));
}
然后我会得到这个错误:
Property 'doc' does not exist on type 'DatabaseSnapshot<unknown>'.
Property 'doc' does not exist on type 'DatabaseSnapshotExists<unknown>'.ts(2339)
Type 'Observable<{ id: string; url: string; description: string; iconUrl: string; courseListIcon: string; longDescription: string; }[]>' is not assignable to type 'Observable<Course>'.
Type '{ id: string; url: string; description: string; iconUrl: string; courseListIcon: string; longDescription: string; }[]' is missing the following properties from type 'Course': id, url, description, iconUrl, and 2 more.ts(2322)
这是模型:
export interface Course {
id: string;
url: string;
description: string;
iconUrl: string;
courseListIcon: string;
longDescription: string;
}
【问题讨论】:
标签: angular firebase firebase-realtime-database rxjs