2

I am working on an Angular4 application where I want to access data in a component I request threw 2 services. When I execute the code in this state the service-call inside my component is not returning any data. When I tried to console.log the value of my service-call the line does not get executed at all.

So my question is, why don't I get a return value from my service function?

I tested the service and it receives the data from my GET-Request.

Can someone help me to get my data into my local variable inside my component and explain to me how it works?

I am pretty new to angular and appreciate any advice of how to improve my code.

Thanks in advance.

The component I want to access the data with

   export class ComponentA implements OnInit {
  public skills: Skill[];

  ngOnInit() {
    this.getColleagueSkills(3);
  }

  getColleagueSkills(colleague_id: number) {
    console.log('function');
    debugger;
    this.skills = this.skillService.getSkillsForColleague(colleague_id);
    console.log('after function');
  }
}

The first service I call to get the data from

@Injectable()
export class SkillsService {
  private SkillUrl = ...;
  private RatingUrl = ...;

  constructor(private http: Http, private colleagueService: ColleagueService) {
  }



getSkillsForColleague(colleague_id: number) : Skill[]{
    let ratedSkillsForColleague = [];
    let colleagueRating = [];
    let colleague: Colleague;
    this.colleagueService.getColleague(colleague_id).subscribe(
      data => { colleague = data},
      error => {},
      () => {
        colleagueRating = colleague.coll_rating;
          for ( let r in colleagueRating){
            ratedSkillsForColleague[r] = colleagueRating[r].rating_skill;
          }
          return ratedSkillsForColleague;
      }
    )
    return ratedSkillsForColleague;
  }

The service I call within the service

@Injectable()
export class ColleagueService {

  private ColleagueUrl = ...;
  constructor(private http: Http) { }


  getColleague(id: number){
    const url = `${this.ColleagueUrl}${id}`
    return this.http.get(url).map(
      response => response.json()).catch(this.handleError);
  }
2
  • Where did you try to console.log? Commented Aug 29, 2017 at 11:58
  • The question is solved but if people are interested in the question where I tried console.log, it was in the function call in my component just after I assigned the response-value to my variable. Commented Aug 29, 2017 at 12:24

1 Answer 1

2

this function:

getSkillsForColleague(colleague_id: number) : Skill[]{
    let ratedSkillsForColleague = [];
    let colleagueRating = [];
    let colleague: Colleague;
    this.colleagueService.getColleague(colleague_id).subscribe(
      data => { colleague = data},
      error => {},
      () => {
        colleagueRating = colleague.coll_rating;
          for ( let r in colleagueRating){
            ratedSkillsForColleague[r] = colleagueRating[r].rating_skill;
          }
          return ratedSkillsForColleague;
      }
    )
    return ratedSkillsForColleague;
  }

...returns and empty array because you immediately return ratedSkillsForColleague before you give getColleague a chance to return the data.

It is best to have getSkillsForColleague return an Observable or Subject and then subscribe to it to have your data returned.

getSkillsForColleague(colleague_id: number) : Subject{
    let subject = new Subject();
    let ratedSkillsForColleague = [];
    let colleagueRating = [];
    let colleague: Colleague;
    this.colleagueService.getColleague(colleague_id).subscribe(
      data => { mitarbeiter = data},
      error => {},
      () => {
        colleagueRating = colleague.coll_rating;
          for ( let r in colleagueRating){
            ratedSkillsForColleague[r] = colleagueRating[r].rating_skill;
          }
          subject.next(ratedSkillsForColleague);
      }
    )
    return subject;
  }
Sign up to request clarification or add additional context in comments.

5 Comments

This seems correct. Just an additional advice though : Try to write your code in english if possible. For foreign people, this code might seems too tedious to follow and understand.
Completely agree, i just copy pasted some stuff around without really looking at it...
Thank you, it worked fine. I read about subjects recently but did not understand enough of how to use them on my own yet. Thank you for your help and an example of how to use subjects.
I will write my code in english from now on or rewrite it if necessary. Thanks alot. Your comments are appreciated.
@Carsten But shouldn't we have business logic in services rather than components? When you subscribe to a Observable, all the business logic has to be moved to component, isn't it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.