Javascript TypeError:无法读取属性';插入数据';未定义的
我得到一个错误: StudentsAttendanceCreateComponent.html:9错误类型错误:无法在students Attention create.component.ts:96 at Array.forEach()读取未定义的属性“insertData”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
谢谢 它的参考误差。如果要在回调中使用组件变量或方法,则应使用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();
}