1

Below code with one api request is working fine. But instead of find in forEach loop, want to get data from another API call, which is not working as it return the values from function and response received later on, as a result view not getting updated.

getResults: Observable<Entity> {
        const entityData= [{id: '3', name:'abc', products: []}, {id: '4', name: 'xyz', products: []}];
        return this.service.getResults().pipe(
        map((data) => this.convert(data, entityData)));
   }

   convert(item, entityData): any {
    let values=[];
    const node: {
      products: item.products ? item.products : [],
      expand: true //getting result(true/false) from another function.
    }
    values.push(node);
    node.products.forEach((childUnit)=> {
        const result = entityData.find(a=>a.id==childUnit.id));

        // want result from another api call, which returns the observable.
        // Once all the result get iterated then want to return the {Values}.
        // this.service.getresultById(item.id).subscribe((data)=>{this.convert(data)})

        const child = this.convert(result, entityData);
        values = values.concat(child);
    });
    return {values};
   }

Any help for the same.

Parveen Sachdeva
  • 989
  • 2
  • 19
  • 28
  • If `convert()` really returns an Observable, you should use [`switchMap`](https://www.learnrxjs.io/learn-rxjs/operators/transformation/switchmap) instead of [`map`](https://www.learnrxjs.io/learn-rxjs/operators/transformation/map). This way, the inner subscription will be subscribed to so you get the data. However, it looks like `convert()` returns an object. – BizzyBob Jul 30 '21 at 13:40
  • @BizzyBob yes convert returns the object with values, I've updated the return type to any now. – Parveen Sachdeva Jul 30 '21 at 13:43
  • maybe you want to try async await? Is it that you want the second api to be performed before returning? – illusion Jul 30 '21 at 13:44
  • @illusion both the API's are returning observable, any help how to do this with observables. need to do await with subscribe ? – Parveen Sachdeva Jul 30 '21 at 13:51
  • Is `item` an array? (*since `node.product = item` and you do `node.product.forEach`*) – BizzyBob Jul 30 '21 at 13:53
  • so basically, you are getting an `observable of observables`. and you want an `observable of objects`, is it? – illusion Jul 30 '21 at 14:50
  • @illusion Doing somekind of manipulation in convert function, which based on other API result, calls the same function(as need to do for child also). Finally returning an object {values}. Values used in html template to display in list. Above code is working fine if do not call another api inside the convert function. – Parveen Sachdeva Jul 30 '21 at 14:53
  • 2
    Why are you calling convert() within convert method recursively? Is this code tested. there are syntax errors like for ex - const result = item.find(a=>a.id==item.id)); – MBB Jul 30 '21 at 15:24
  • @MBB Yes it's working fine without another API call, it's tested and pasted here as a reference. Need to set expand property for child also, that's why it's recursive. – Parveen Sachdeva Jul 30 '21 at 15:31
  • I think more context would help. Like others have said. You are treating `item` as both an object `item.products` and an array `item.find(x)` simultaneously. – hyperdrive Jul 30 '21 at 16:31
  • @hyperdrive I've updated. Point is right now using array.find to get the result and it's working fine for this. But want this result from API call. – Parveen Sachdeva Jul 30 '21 at 16:43
  • I still don't understand what you're trying to do. But regardless, the rxjs map operator won't subscribe to any inner observables. So you will need to use a different operator that does. SwitchMap, etc. There's like 100 operators so choosing the right one depends on what result you want, which is where everyones confusion comes from. See: https://stackblitz.com/edit/angular-ivy-lyxxcv?file=src/app/app.component.ts – hyperdrive Jul 30 '21 at 17:55
  • I'm not sure exactly what your trying to achieve. If you want to make another call then you can use switchMap() if you have more then one call to make the use Observable.combineLatest. If you could, please post your code on stackblitz. – VVS Aug 19 '21 at 12:29

0 Answers0