Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
JHipster Angular use output of query as input of next query

I am trying to write a code where an Angular Course Service receives the login id of a student to list all courses where the student enrolls. This result is then piped to another service which takes the output of the first query as its input.

teachers: ITeacher[];
this.courseService.query({
  'stdId.equals': this.account?.login,
}).pipe(
  map(data=> {
    this.teacherService.query({
      'courseId.in': data.body, 
    }).subscribe((res: HttpResponse<ITeacher[]>) => {
      this.teachers = res.body;
    })
  })
);

The problem is that I am able to see the results in data.body, but I cannot get the actual values of the data. Eg. If I try to get the values as data.body.id, I get an error message saying there is no field id in the Class or Interface. I am new to Angular so these are my questions:

  1. Is this the correct way to do what I am planning to do? If not, what is the correct way?
  2. How do I get the values of the specific fields like id from the data.body?

Any help is much appreciated.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I strongly suggest that you use an Observable as your final "target". Instead of having teachers as a static array, you would have teachers$, an Observable, which you would consume in the DOM with the async pipe or elsewhere in your code. Also, you want to avoid subscribing to Observables inside Observable pipes (nested subscribe calls are BAD code smell).

Here is my suggested approach:

teachers$: Observable<ITeacher[]>;
this.teachers$ = this.courseService.query({
  'stdId.equals': this.account?.login,
}).pipe(
  switchMap(data => this.teacherService.query({
    'courseId.in': data.body, 
  }))
);

When you need to subscribe to a child Observable from inside a parent Observable (like in this case), you should use a higher-order mapping operator like switchMap, concatMap, mergeMap or exhaustMap. These operators differ mostly in their concurrency models. For this case, depending on how many requests could potentially be made, you may want to change from switchMap to something else.

over 4 years ago · Santiago Trujillo Report

0

Embracing the suggestion from this answer, I'd just add the solution to the array of course ids as follows:

teachers$: Observable<ITeacher[]>;
this.teachers$ = this.courseService.query({
  'stdId.equals': this.account?.login,
}).pipe(
  map(data => data.body.map(({id}) => id)),
  map(data => this.teacherService.query({
    'courseId.in': data, 
  }))
);

I didn't test it but I think you can try this.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!