Having some trouble on calling an API and using it for options

Viewed 46

I am trying to call an existing API that returns a ID and Name as a Number and String and use the ID to route to different pages, but not sure where I am going wrong

 ngOnInit(): void {
    this.getData();
    // calling the coach-interaction service getCategories endpoint

    // use response to work out the flow based on the the conditionals of the issue

    // if categories count = 0 then error
    // if (categoryID == 0){
    //   this.appService.openSnackBar('You don not have access to this Service');
    // }else if (categoryID == 1){
    // // if categories count = 1 then set the category id in the payload and route to that category
    // } else if (categoryID > 1) {
    // // if categories count > 1 then show this component/page
    // }
  }
  goBackTo = () => this.router.navigate([`${appRoutesNames.COACH_INTERACTION}/${coachInteractionRouteNames.MATCH_MAKING}`]).then();


  getData = () =>{

  this.http.get(this.coachService.GetCategories().subscribe)
   .map((res:Response) => (
        res.json()
   ))
   .subscribe((data: any) => {console.log(data)})
  }
1 Answers

Use regular functions within your components, instead of arrow function. The scope to which this is applied is different between a regular function and an arrow function.

getData() {
  this.http.get("YOUR_ENDPOINT")
    .subscribe(data => console.log(data))
}

goBackTo() {
  this.router.navigate(...)
}
Related