Hey everyone, I have a scenario where I'm working with 2 MySQL tables: itemsClass which holds various classes, and itemType which links to itemClass and contains type values for a specific class.
My goal is to create a service that returns an Observable<Item[]>, by combining two http.get Observables in accordance with this interface:
interface Item{
itemcClassId:any; //itemClass.id, fetched from itemClass select query
itemClassName:any; //itemClass.name fetched from itemClass select query
itemTypeValue?:any; //from itemType select, if itemType has only one value related to the first table's class
itemTypeValues?:any[]; //if there are multiple values in the itemType table
}
After extensive searching, I found a code structure similar to this:
getEmploye(name: string): Observable<any> {
return this.getClass(name).pipe(
switchMap((response) => {
return forkJoin(of(response),this.getType(class.id);),
map(res=>{
return {...res[0],values:...res[1]}
})
})
)
}
However, this code snippet only returns a single item whereas I need an array of items. Any suggestions?
Thanks in advance!
EDIT: The code I posted was generic and not customized. A tailored version that retrieves just one item would involve something like this:
getItem(id: number): Observable<Item> {
return this.http.get(url_to_ItemClass_rest_api).pipe(
switchMap((item) => {
return forkJoin(of(item),
this.http.get(url_to_ItemType_rest_api_filtered_by_foreign_key_item.id/);),
map(res=>{
return {...res[0],values:...res[1]}
})
})
)
}