Undefined varibale already declared whet getting data from api on ngOnInit

风格不统一 提交于 2020-05-17 06:36:31

问题


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 ?


回答1:


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!




回答2:


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)
  );
}


来源:https://stackoverflow.com/questions/60526291/undefined-varibale-already-declared-whet-getting-data-from-api-on-ngoninit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!