【发布时间】:2021-07-26 22:44:30
【问题描述】:
我正在从返回 Observable 的 API 获取数据(students 通过getStudents())。在这个结果中,我需要从两个不同的表中获取数据并组合结果。
这是我的简化界面:
export interface student Student {
id: string
name: string,
school_id: string,
classroom_id: string
}
export interface school School {
id: string,
name: string
}
export interface classroom Classroom {
id: string,
name: string
}
我现在需要获取所有students 并通过外键school_id 和classroom_id 为每个student 添加相应的school 和classroom。
我目前的方法类似于以下内容。我知道它尚未完成,但我无法找到合适的运营商以及如何正确使用它。
this.getStudents().pipe(
switchMap(students => {
student.map(student => forkJoin([
this.getSchool(student.school_id),
this.getClassroom(student.classroom_id)
]))
})
)
所有方法(getStudents()、getSchool()、getClassroom())都返回 Observables。我的目标是在订阅后获得一组students 以及相应的school 和classroom 数据。
如果我需要获取单个学生(例如使用getStudent())然后使用combineLatest 组合多个流,我知道如何完成它。但是当获取多个students 时,情况就不同了。
提前谢谢你!
【问题讨论】:
标签: javascript typescript rxjs observable