Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';插入数据';未定义的_Javascript_Json_Angular_Search - Fatal编程技术网

Javascript TypeError:无法读取属性';插入数据';未定义的

Javascript TypeError:无法读取属性';插入数据';未定义的,javascript,json,angular,search,Javascript,Json,Angular,Search,我得到一个错误: StudentsAttendanceCreateComponent.html:9错误类型错误:无法在students Attention create.component.ts:96 at Array.forEach()读取未定义的属性“insertData” 谢谢 它的参考误差。如果要在回调中使用组件变量或方法,则应使用lambda(箭头)函数,而不是使用文字或匿名函数。forEach采用回调函数,除非绑定它,否则此在回调中不可用。一种方法是通过自动绑定它 Object.ke

我得到一个错误:

StudentsAttendanceCreateComponent.html:9错误类型错误:无法在students Attention create.component.ts:96 at Array.forEach()读取未定义的属性“insertData”


谢谢

它的参考误差。如果要在回调中使用组件变量或方法,则应使用lambda(箭头)函数,而不是使用文字或匿名函数。

forEach
采用回调函数,
除非绑定它,否则此
在回调中不可用。一种方法是通过自动绑定它

Object.keys(form.value).forEach((key) => {
  if (key !== 'cursoId') {
    const json = {
      EstudianteId: key,
      asistencia: form.value[key],
      cursoId: cursoID
    };
    this.insertData(json);
  }
});
}

为了在回调函数中使用
this
关键字,您应该使用箭头函数,您的代码应该如下所示:

submitForm(form: NgForm) {

let cursoID: number;

Object.keys(form.value).forEach((key) => {
  if (key === 'cursoId') {
    cursoID = form.value[key];
  }
});

Object.keys(form.value).forEach((key) => {
  if (key !== 'cursoId') {
    const json = {
      EstudianteId: key,
      asistencia: form.value[key],
      cursoId: cursoID
    };
    this.insertData(json);
  }
});
}



insertData(json: any) {
this.service.studentsAsistencia(json)
  .subscribe(() => {
    this.openSnackBar('Datos insertados correctamente.', 'OK');
  });
// this.clearForm();
}
您可以在此处了解这些差异:

尝试切换到箭头功能,可能重复
submitForm(form: NgForm) {

let cursoID: number;

Object.keys(form.value).forEach((key) => {
  if (key === 'cursoId') {
    cursoID = form.value[key];
  }
});

Object.keys(form.value).forEach((key) => {
  if (key !== 'cursoId') {
    const json = {
      EstudianteId: key,
      asistencia: form.value[key],
      cursoId: cursoID
    };
    this.insertData(json);
  }
});
}



insertData(json: any) {
this.service.studentsAsistencia(json)
  .subscribe(() => {
    this.openSnackBar('Datos insertados correctamente.', 'OK');
  });
// this.clearForm();
}