0

I have the following code:

ngOnInit(): void {
  this.issueService.getIssues().pipe(
    switchMap(issues => {
      this.issuesList = issues;
      const observables = this.issuesList.map(issue => this.issueService.getChildrenIssues(issue.id));

       return forkJoin([observables]);
     })
  ).subscribe(
  (...results) => {
    results.map((result, i) => {
      this.childIssueMap.set(this.issuesList[i].id, result);
      // error: Argument of type '[Observable<Issue[]>]' is not assignable to parameter of type 'Issue[]'.
    });
  }
);
}

Where:

public issuesList: Issue[];
public childIssueMap = new Map<string, Issue[]>();

I need to get all my issues from a server by calling the "getIssues()" first. Then, when I get them, for each of them I need to call a service "getChildrenIssues(parentId)" where I pass the issue id and I get all the children issue. Then I store the children issues in the childIssueMap by using the parent id as the key and the list of children issues as value.

I am not really sure how to do this. I tried to use forkJoin but my editor says it's deprecated, therefore I used the variation with [] but this time I am getting the error you see above.

Biruk
  • 23
  • 1
  • 5
Rexam
  • 823
  • 4
  • 23
  • 42
  • The deprecation means that you should use forkJoin([a,b]) instead of forkJoin(a,b) – Alexander Nov 19 '20 at 12:12
  • Cold you provide an example? Also, check my edit bacause I think I am using forkJoin the wrong way. – Rexam Nov 19 '20 at 12:19
  • You currently pass an array with a single element, namely an array with whatever getChildrenIssues returns elements to your forkJoin. – Lolmewn Nov 19 '20 at 12:23
  • Does this answer your question? [forkJoin is deprecated: resultSelector is deprecated, pipe to map instead](https://stackoverflow.com/questions/52486786/forkjoin-is-deprecated-resultselector-is-deprecated-pipe-to-map-instead) – Liam Nov 19 '20 at 12:24
  • You also shouldn't chain subscriptions. Use `pipe()` instead – Liam Nov 19 '20 at 12:25
  • @Liam could you provide an example? The question you linked doesn't show how I'm suppose to handle the result of the inner call. Also, I don't know how many times I need to call my service because of the issueList may vary. – Rexam Nov 19 '20 at 12:28

1 Answers1

0

I solved this issue by using forkJoin the following way:

this.issueService.getIssues().pipe(
  switchMap(
    issues => {
       this.issuesList = issues;
       const observables = this.issuesList.map(issue => this.issueService.getChildrenIssues(issue.id));
       const source = forkJoin(observables);

       return source;
  })
).subscribe(
  (children) => children.forEach((child, i) => {
    this.childIssueMap.set(this.issuesList[i].id, child);
  })
);
Rexam
  • 823
  • 4
  • 23
  • 42