Typescript / Angular2 TypeError:无法读取未定义的属性

乔·伯特洛

Ang2组件:

import { Component, OnInit } from '@angular/core';
import { AngularFire, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2';
import Chart from 'chart.js'

@Component({
  selector: 'app-prchart',
  templateUrl: './app-prchart.component.html',
  styleUrls: ['./app-prchart.component.css']
})
export class AppPRChartComponent implements OnInit {
  userEmail: any;
  email: string;
  uid: any;
  records = [];
  newRecords = [];
  filteredRecords = [];
  labels = [];

  public barChartOptions:any = {
    scaleShowVerticalLines: false,
    responsive: true,
    legend: {
        display: false
     }
  };
  public barChartColors: any [] =[
    {
        backgroundColor:'rgba(30, 136, 229, 1)'
    }
  ];

  public barChartType:string = 'bar';

  constructor(public af: AngularFire) {
    var auth = this.af.auth.subscribe( (user) => {
      if (user) {


        this.userEmail = this.af.auth.subscribe(auth => {
          const queryObservable = af.database.list('/users/'+ auth.auth.uid +'/records/', {
          });
          queryObservable.subscribe(queriedItems => {
            this.records.push(queriedItems);
          });

          // Filter records into PR's
          this.newRecords =
          this.records[0].sort((a, b) => {
            if (a.movement === b.movement) {
              return a.weight >= b.weight ? -1 : 1
            }
            return a.movement > b.movement ? 1 : -1
          })
          .filter((rec, i, arr) => {
            if (i === 0) return true
            return rec.movement !== arr[i - 1].movement
          });
          let recordString = JSON.stringify(this.newRecords);
          let recordParse = JSON.parse(recordString);
          this.filteredRecords.push(recordParse);
        });
      } else {
      }
    });

    this.filteredRecords[0].forEach(function(snapshot) {
        this.labels.push(snapshot.movement);
        //barChartData.push(snapshot.weight);
    });
    //console.log(barChartLabels);
    //console.log(barChartData);
  }

  ngOnInit() {

  }

}

我正在尝试将项目推送到数组中,但不断出现以下错误:

TypeError: Cannot read property 'labels' of undefined

错误发生在运行此代码行的底部:

this.labels.push(snapshot.movement);

我已经玩了几个小时了,无法弄清楚我在做什么错,对您的任何帮助表示感谢。

萨拉瓦那

问题是this回调函数内部发生了变化。您可以使用箭头功能来解决此问题,该功能将捕获正确的内容this

this.filteredRecords[0].forEach((snapshot) => {
    this.labels.push(snapshot.movement);
    //barChartData.push(snapshot.weight);
});

或者通过捕获this另一个变量:

let that = this;
this.filteredRecords[0].forEach(function (snapshot) {
    that.labels.push(snapshot.movement);
    //barChartData.push(snapshot.weight);
});

这可能很有用:TypeScript中的“ this”是什么

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Angular2:无法读取未定义的属性“名称”

angular2:错误:TypeError:无法读取未定义的属性“ ...”

Angular TypeError:无法读取未定义的属性“ then”

TypeError:无法使用Angular读取未定义的属性“ comments”

Angular2,TypeScript,如何将属性值读取/绑定到组件类(在ngOnInit中未定义)

Angular2:TypeError:无法读取未定义的属性“ Symbol(Symbol.iterator)”

Angular2无法读取未定义的属性“ push”

无法读取未定义[Angular2]的属性“ isStopped”

Angular 2 TypeError:无法读取未定义的属性“ toUpperCase”

无法读取未定义angular2的属性

无法读取Angular2 Typescript中未定义的属性“值”

Angular 6 TypeError:无法读取未定义的属性“ call”

NgZone / Angular2 / Ionic2 TypeError:无法读取未定义的属性“运行”

TypeError:无法读取angular2中未定义的属性“ 0”以在datepicker中使用索引

无法读取Typescript Angular 5中未定义的属性somevalue

Angular Ionic3 TypeScript-从数组拼接后出现“ TypeError:无法读取未定义的属性'名称'”

Angular-TypeError:无法读取未定义的属性“名称”

Typescript,React:TypeError:无法读取未定义的属性“ push”

无法读取未定义的Angular / TypeScript属性'forEach'

Angular TypeError无法读取未定义的属性“ then”

TypeScript:TypeError:无法读取未定义的属性“ push”

Webpacked Angular2应用程序TypeError:无法读取未定义的属性“ getOptional”

NativeScript/Angular - TypeError:无法读取未定义的属性“符号”

Angular - TypeError:无法读取未定义的属性“推送”

Angular - TypeError:无法读取未定义的属性“订阅”

(Angular2 和 Typescript)Angular 的 DatePicker 无法读取未定义的属性“切换”

无法读取未定义的 Angular/Typescript 的属性“推送”

Angular 6:TypeError:无法读取未定义的属性“值”

无法读取未定义的 Typescript / Angular 6 的属性“推送”