0

I'm trying to fetch data from nodeJS API with angular, I have a variable declared and I want to affect the response from server to it, here is my code :

export class SondageSingleComponent implements OnInit, AfterViewInit {
  @ViewChild('containerPieChart') element: ElementRef;
  survey: any = {};
  currentUser: any;
  statistics: any;

  colorCounter: any = 0;
  d3Colors: any[] = ["#3182bd", "#6baed6", "#9ecae1", "#c6dbef", "#e6550d"];

  private host: D3.Selection;
  private svg: D3.Selection;
  private width: number;
  private height: number;
  private radius: number;
  private htmlElement: HTMLElement;
  private pieData = [];

  constructor(
    private http: HttpClient,
    private route: ActivatedRoute,
    private router: Router,
    private toastr: ToastrService
  ) { }

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.http.get('/api/surveys/' + params.id).subscribe(survey => {
        this.survey = survey;
       // console.log(this.survey)
       debugger
        this.http.get('/api/surveys/getStatistics/' + params.id).subscribe(statistics => {
          this.statistics = statistics;
          this.statistics.options.forEach(option => {
            this.pieData.push(option.number);
            option.color = this.d3Colors[this.colorCounter];
            this.colorCounter = this.colorCounter + 1;
          });
        });
      }, error => {
        if (error.status === 400) {
          this.showError(error.error.error, 'Erreur!');
          if (error.error.error === 'Le sondage demandé n\'existe plus!') {
            this.router.navigateByUrl('/sondage');
          }
        }
      });
    });

  }

the data coming successfully from node side and when I try to affect the data to survey variable and to try, any one can tell why can't read this.survey ?

547n00n
  • 1,356
  • 6
  • 30
  • 55
  • What response did you get when you do ```console.log(this.survey)```?? – Maniraj Murugan Mar 04 '20 at 12:43
  • Can we have some more information? is the debugger actually being triggered? can we see a screenshot of the chrome debugger paused on the breakpoint? and what @ManirajMurugan said – Tom Mar 04 '20 at 12:44

2 Answers2

0

This is how I would've implemented it (take a look at the code comments)

ngOnInit() {
    this.route.params
        .subscribe(params => {
            this.http.get('/api/surveys/' + params.id)
                .pipe(
                    take(1),
                    // using switchMap is a lot better than subscribing
                    switchMap(survey => {
                        this.survey = survey;
                        return this.http.get('/api/surveys/getStatistics/' + params.id)
                    })
                )
                .subscribe(statistics => {
                    const newPieData = []
                    // using .map is a better use of javascript
                    statistics.options = statistics.options
                        .map(option => {
                            newPieData.push(option.number);
                            this.colorCounter = this.colorCounter + 1;
                            option.color = this.d3Colors[this.colorCounter];
                            return option;
                        });
                    this.pieData = [...this.pieData, ...newPieData];
                    // change this.statistics reference only at the end
                    // a change triggers the angular cycle
                    this.statistics = statistics;
                }, error => {
                    if (error.status === 400) {
                        this.showError(error.error.error, 'Erreur!');
                        if (error.error.error === 'Le sondage demandé n\'existe plus!') {
                            this.router.navigateByUrl('/sondage');
                        }
                    }
                });
        });
}

I'd really recommend using types.

Hope this helps!

Ben Ari Kutai
  • 509
  • 3
  • 9
0

Golden rule with observables: Don't nest subscriptions!

It looks like you want to:

  1. Listen for route param changes
  2. Make 2 http requests based on a route param
  3. Update a chart based on the http responses

Listening to this.route.params is a good start for #1.

Secondly, use switchMap to run a new observable. And use forkJoin to call multiple observables in parallel.

ngOnInit() {
  this.route.params.pipe(
    switchMap(params => forkJoin({
      survey: this.http.get('/api/surveys/' + params.id),
      statistics: this.http.get('/api/surveys/getStatistics/' + params.id)
    }))
  ).subscribe(result => {
    this.survey = result.survey;
    this.statistics = result.statistics;
    this.updateChart(result.statistics);
  }, 
    error => this.handleError(error)
  );
}

private handleError(error) {
  if (error.status === 400) {
    this.showError(error.error.error, 'Erreur!');
    if (error.error.error === 'Le sondage demandé n\'existe plus!') {
      this.router.navigateByUrl('/sondage');
    }
  }
}

private updateChart(statistics) {
  statistics.options.forEach(option => {
    this.pieData.push(option.number);
    option.color = this.d3Colors[this.colorCounter];
    this.colorCounter = this.colorCounter + 1;
  });  
}

DEMO: https://stackblitz.com/edit/angular-m4agxv

Angular < 8

forkJoin({}) is only usable since RxJS 6.5 (Angular >= 8). For earlier versions you will have to pass in an array of observables.

ngOnInit() {
  this.route.params.pipe(
    switchMap(params => forkJoin([
      this.http.get('/api/surveys/' + params.id),
      this.http.get('/api/surveys/getStatistics/' + params.id)
    ]))
  ).subscribe(result => {
    this.survey = result[0];
    this.statistics = result[1];
    this.updateChart(result[1]);
  }, 
    error => this.handleError(error)
  );
}
Kurt Hamilton
  • 12,490
  • 1
  • 24
  • 40
  • thanks for your help, I got this error : no overload matches this call argument of type '{suervey: Observable; statistics: Observable } is not assignable to parameter of type ObservableInput – 547n00n Mar 04 '20 at 14:20
  • What version of Angular are you using? – Kurt Hamilton Mar 04 '20 at 14:30
  • I've updated my answer to show how you should use `forkJoin` with Angular < 8 – Kurt Hamilton Mar 04 '20 at 14:39
  • thanks for the update but I want to ask where to save coming data from api call to display survey object attribute in the html file !!! – 547n00n Mar 04 '20 at 14:50
  • I thought that was implied in the answer? In the subscribe - where `result.survey` is being logged to the console. I've updated my answer to make it blindingly obvious. – Kurt Hamilton Mar 04 '20 at 14:52
  • not working for me, it generate a 500 server error, I put a debugger at the start of ngOnInit and Handle error function and directly execute handleError function – 547n00n Mar 04 '20 at 15:52
  • 500 is an internal server error - nothing to do with your javascript. – Kurt Hamilton Mar 04 '20 at 16:07
  • Here is a demo (using mock observable calls instead of http calls) that demonstrates a live version of this code: https://stackblitz.com/edit/angular-m4agxv. The issue is on the backend – Kurt Hamilton Mar 04 '20 at 16:29