Reputation: 532
This is older implementation and I wanted to get HeatingDesired value
//1st api call
this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', this.refs.roomRef).subscribe(({ rows }) => {
if (rows.length > 0) {
rows.forEach((row) => {
let pointId = this.helperService.stripHaystackTypeMapping(row['id']).split(' ')[0];
//2nd api call
this.siteService.getHisPointData(pointId, 'current').subscribe(({ rows, meta }) => {
let metaVal = meta;
if (rows.length > 0) {
let HeatingDesired = this.helperService.stripHaystackTypeMapping(rows[0].val);
}
});
});
}
});
==========================================================================
Now I am trying to use switchmap and subscribe to the final response
getDesiredHeatingTemp(eleId){
//1st api call
return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId).pipe(
switchMap((a)=>{
a.forEach((m) => {
let pointId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
//2nd api call
return this.siteService.getHisPointData(pointId, 'current')
})
}))
}
this.getDesiredHeatingTemp(eleId).subscribe((a) => console.log(a))
But is gives me error saying
Argument of type '(a: any) => void' is not assignable to parameter of type '(value: any, index: number) => ObservableInput<any>'.
Type 'void' is not assignable to type 'ObservableInput<any>'.
The 2nd api call expects parameter which I get from 1st api call
Update
The response which I get from 1st api call looks like
a=
cols: (23) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
meta: {ver: "2.0"}
rows: [{…}]
a=
cols: (23) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
meta: {ver: "2.0"}
rows: Array(1)
0:
air: "m:"
desired: "m:"
id: "r:5d90685e647845530d95ad0a nizam346_2-VAV-9600-desiredTempHeating"
kind: "Number"
__proto__: Object
length: 1
__proto__: Array(0)
From the above response of 1st api call I want to get rows[0].id and pass it on to the 2nd api call.
I tried using map, foreach but on debugging the execution doesn't go inside const obs = a.forEach((m)=>{ let pointId = this.helperService.stripHaystackTypeMapping(m.rows[0]['id']).split(' ')[0];
getDesiredHeatingTemp(eleId){
return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId)
.pipe(
switchMap(a => {
const obs = a.forEach((m)=>{
let pointId = this.helperService.stripHaystackTypeMapping(m.rows[0]['id']).split(' ')[0];
return this.siteService.getHisPointData(pointId, 'current')
})
return forkJoin(obs)
}))
}
Upvotes: 0
Views: 198
Reputation: 57919
swithmap must return an observable
getDesiredHeatingTemp(eleId){
//1st api call
return this.siteService.getScheduleParamsByRoomRef('temp and air and desired and heating', eleId).pipe(
switchMap((a)=>{
//you formed a unique observable using forkjoin
const obs=a.map((m)=>{
let pointId = this.helperService.stripHaystackTypeMapping(m['id']).split(' ')[0];
return this.siteService.getHisPointData(pointId, 'current')
})
//in obj we has,e.g. [
// this.siteService.getHisPointData(1,'current'),
// this.siteService.getHisPointData(1,'current'),
// this.siteService.getHisPointData(1,'current')
// ]
return forkJoin(obs)
}))
}
Upvotes: 2