Невозможно прочитать свойство 'map' из undefined, используя D3 Bar Chart в Angular 7 - PullRequest
0 голосов
/ 04 июня 2019

Невозможно прочитать свойство 'map' из неопределенного, когда я отображаю объект totalStores.

Я проанализировал существующие данные JSON в две переменные store и storeCount, объект хранилища для оси X и storeCount для оси Y

ngOnInit() {
    this.DashboardService.getActiveProjectsStatusByDimension(
      this.statusIndex,
      this.dimension
    ).subscribe(res => {
      this.res = res;
      this.projectSelectedDimension = JSON.parse(JSON.stringify(res));

      this.totalStores = Array();

      var storeCountIndex = 2;

      for (let k = 0; k < this.projectSelectedDimension.data.length; k++) {
        let obj = this.projectSelectedDimension.data[k];
        let xAxis = Object.keys(obj)[0];
        let yAxis = obj[Object.keys(obj)[0]][storeCountIndex];
        // console.log("x:", xAxis);
        // console.log("y:", yAxis);
        let objBarChart = { store: xAxis, storeCount: yAxis };
        this.totalStores.push(objBarChart);
      }

    });

    this.initAxis();
  }


private initAxis() {
    this.x = d3Scale
      .scaleBand()
      .rangeRound([0, this.width])
      .padding(0.1);
    this.y = d3Scale.scaleLinear().rangeRound([this.height, 0]);
    this.x.domain(this.totalStores.map(d => d.store));
    this.y.domain([
      0,
      d3Array.max(this.totalStores, this.totalStores.map(d => d.storeCount))   // erroe saying valueof is not a function
    ]);
  }


1 Ответ

0 голосов
/ 04 июня 2019

Код внутри функции .subscribe () запускается ПОСЛЕ возврата функции getActiveProjectsStatusByDimension ().Итак, вы пытаетесь отобразить неопределенный объект.Чтобы решить эту проблему, просто поместите вашу функцию initAxis () в блок подписки.

ngOnInit() {
    this.DashboardService.getActiveProjectsStatusByDimension(
      this.statusIndex,
      this.dimension
    ).subscribe(res => {
      this.res = res;
      this.projectSelectedDimension = JSON.parse(JSON.stringify(res));

      this.totalStores = Array();

      var storeCountIndex = 2;

      for (let k = 0; k < this.projectSelectedDimension.data.length; k++) {
        let obj = this.projectSelectedDimension.data[k];
        let xAxis = Object.keys(obj)[0];
        let yAxis = obj[Object.keys(obj)[0]][storeCountIndex];
        // console.log("x:", xAxis);
        // console.log("y:", yAxis);
        let objBarChart = { store: xAxis, storeCount: yAxis };
        this.totalStores.push(objBarChart);
      }
      this.initAxis() //put it here
    });

//    this.initAxis();
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...