I am retrieving data (students through getStudents()) from an API that returns an Observable. Within this result, I need to obtain data from two different tables and merge the information.
Below are my simplified interfaces:
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
}
My task now is to fetch all students and add the corresponding school and classroom for each student using the foreign keys school_id and classroom_id.
Currently, my approach resembles the following code snippet. It is incomplete as I am struggling to find the appropriate operator and its correct usage.
this.getStudents().pipe(
switchMap(students => {
student.map(student => forkJoin([
this.getSchool(student.school_id),
this.getClassroom(student.classroom_id)
]))
})
)
All of the methods (getStudents(), getSchool(), getClassroom()) return Observables. My objective is to receive an array of students with the respective school and classroom data after subscribing.
I understand how to achieve this when fetching a single student (e.g. with getStudent()) and then combining multiple streams using combineLatest. However, it differs when retrieving multiple students.
Thank you in advance!