case1
报错代码
quizList[index] = currentQuiz;
quizList[index].learningItem = item;
报错内容
Cannot read properties of undefined (reading 'learningItem')
报错原因
currentQuiz对象有可能是undefined,如果再给undefined对象quizList[index]动态添加属性learningItem,会导致出现cannot set property xxx of undefined报错
解决方法
quizList[index] = currentQuiz == undefined ? {} : currentQuiz;
quizList[index].learningItem = item;
判断一下quizList[index]是否是undefined即可
case2
报错代码
quiz.finishedstudent.forEach(item => {
if (item.score < 80) {
errorNum++;
}
})
报错内容
Cannot read properties of undefined (reading 'forEach')
解决方法
if (quiz.finishedstudent != undefined) {
quiz.finishedstudent.forEach(item => {
if (item.score < 80) {
errorNum++;
}
})
}