【发布时间】:2018-01-29 21:28:29
【问题描述】:
我正在尝试从数据库中读取时间表,但如果星期一是公共假期,我会收到错误 Uncaught TypeError: Cannot read property 'subjects' of undefined
我正在调用'subject' 的 JQuery 行是:
var currentsubjectone = timetableday.periods[index].subjects[0],
currentsubjecttwo = timetableday.periods[index].subjects[1];
被调用的函数: 函数 updateTeacherTimetableClassDetails(timetableday, periodslots, date) { var lastmondayfordate = getLastMonday(日期);
periodslots.each(function(index){
// Testing
if (typeof timetableday.periods[index].subjects[0] === undefined ){
timetableday.periods[index].subjects[0] == '*';
}
if (timetableday.periods[index].subjects[1] === undefined ){
timetableday.periods[index].subjects[1] == '*';
}
//testing
var currentsubjectone = timetableday.periods[index].subjects[0],
currentsubjecttwo = timetableday.periods[index].subjects[1];
var lineindentifier = [];
subjectcode = [];
roomcode = [];
if (currentsubjectone != undefined)
{
lineindentifier.push(currentsubjectone.lineidentifier);
subjectcode.push(currentsubjectone.subjectcode);
roomcode.push(currentsubjectone.roomcode);
}
if (currentsubjecttwo != undefined)
{
lineindentifier.push(currentsubjecttwo.lineidentifier);
subjectcode.push(currentsubjecttwo.subjectcode);
roomcode.push(currentsubjecttwo.roomcode);
}
var classdetail = $(this).find('.class-detail');
classdetail.empty();
classdetail.append($('<div class="line-identifier">' + lineindentifier.join('<br />') + '</div><div class="subject-code">' + subjectcode.join('<br />') + '</div><div class="room-code">' + roomcode.join('<br />') + '</span></div>'));
var perioddom = this;
$(perioddom).find('.attendance-checklist').empty();
console.log(subjectcode);
// add in attendance checklist if there is a valid subject
if (subjectcode.length > 0)
{
// attendance checklist might not be available, so we build some code to run when it is
loadAttendanceChecklistAndRunFunction(g_loginkey, g_selectedteacher.teachercode, g_selectedyear, function() {
var currentweekchecklist = g_teacherattendancechecklist.weeks[lastmondayfordate.getTime()],
checklist = undefined;
if (currentweekchecklist != undefined && currentweekchecklist.days.length > 4)
{
checklist = currentweekchecklist.days[date.getDay() - 1];
if (checklist.checklist[index] == 'Y')
updateTeacherTimetableAttendanceChecklist(index + 1, true, perioddom, date);
else
updateTeacherTimetableAttendanceChecklist(index + 1, false, perioddom, date);
}
else
{
updateTeacherTimetableAttendanceChecklist(index + 1, false, perioddom, date);
}
});
}
});
}
当我加载页面时,它在星期一不是公共假期并且有数据时效果很好,但是当星期一是公共假期时,没有数据被发送,因此它会生成 TypeError
我尝试过的:typeof(在上面的代码中)并检查 null。
任何指导都会很棒。
谢谢!
【问题讨论】:
-
当
subjects未定义时不能做subjects[0] -
所以我需要先定义一个空白数组?
-
可能类似于:
if(Array.isArray(subjects) && subjects.length) -
那不是
currentsubjectone != undefined@charlietfl 吗? -
No.. 试图建议您首先检查
subjects是什么
标签: jquery jquery-mobile