[英]why I get Uncaught TypeError: Cannot read property 'sequence' of undefined
Here is a code to track and handle the progress of the user through my web app slides, everything seems to work fine till I reach the 9th execution of the function.这是通过我的网络应用程序幻灯片跟踪和处理用户进度的代码,在我执行该函数的第 9 次之前,一切似乎都正常。 at that point I get this error:那时我收到此错误:
Uncaught TypeError: Cannot read property 'sequence' of undefined未捕获的类型错误:无法读取未定义的属性“序列”
Unfortunately, I don't know how to debug the code or realize what causes the error and I need a hand to fix it.不幸的是,我不知道如何调试代码或意识到导致错误的原因,我需要有人来修复它。
It seems that this for loop causes the error but why?似乎这个 for 循环会导致错误,但为什么呢?
// Fill planedSlides array
for(let a = 1; a < current_slide; a++){
if(slides[a - 1].sequence == nextSequence && slides[a - 1].plan == true){
console.log('a: ' + a)
planedSlides.push(a);
}
}
// Global Variables initiate at course start and modified throught the slides // sequence A =>> serie 1 ---- serie 1 ---- serie 3 ---- serie 4 var slides = [ { id: 1, difficulty: 2, performance: 20, guided_phrases: [], sequence: "A", plan: false, planned: 0 }, { id: 2, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: true, planned: 0 }, { id: 3, difficulty: 4, performance: 40, guided_phrases: [], sequence: "A", plan: false, planned: 0 }, { id: 4, difficulty: 4, performance: 20, guided_phrases: [], sequence: "B", plan: true, planned: 0 }, { id: 5, difficulty: 4, performance: 50, guided_phrases: [], sequence: "B", plan: true, planned: 0 }, { id: 6, difficulty: 2, performance: 20, guided_phrases: [], sequence: "A", plan: false, planned: 0 }, { id: 7, difficulty: 4, performance: 30, guided_phrases: [], sequence: "A", plan: true, planned: 0 }, { id: 8, difficulty: 4, performance: 40, guided_phrases: [], sequence: "B", plan: false, planned: 0 }, { id: 9, difficulty: 4, performance: 20, guided_phrases: [], sequence: "B", plan: false, planned: 0 }, ]; var current_slide; // assigned at each slide start let lastViewedSlide; let lastSlide = slides.length; // last slide of the course let courseEnded = false; // Test Assignments current_slide = 1; // Global Variable of progress() let queue = []; // At each end of slide we just initiate the progress function.. function progress(e){ // Check if we are in intro or outro if(e){ switch(e){ // Intro slides with negative numbers incrementing.. slide 0 is the first slide.. case -1: goToSlide(-1); break; case -2: goToSlide(-2); break; case -3: goToSlide(-3); break; // Outro slides with positive number beyond 100.. case 101: goToSlide(101); break; case 102: goToSlide(102); break; case 103: goToSlide(103); break; // last intro slide executes progress like this: progress(1); case 1: // Go to first slide and generate queue const firstSequence = slides[0].sequence; let iterate = 0; while(slides[iterate].sequence == firstSequence){ iterate++ queue.push(iterate); } goToSlide(1); break; }; return; // return the whole progress function if we are in intro or outros.. } // Check if we have reached last course slide if(current_slide == lastSlide){ courseEnded = true; // it'll remain true }; if(courseEnded === true){ progressCourseEnded(); return; }; // initiate progress here queue.shift(); // Remove this slide from queue if(queue.length !== 0){ goToSlide(queue[0]); // if the queue is NOT empty go to the first element of queue } else { // If the queue is empty fill it.. fillQueue(); // Then view the queue slide by slide goToSlide(queue[0]); } // fillQueue function to generate queue again function fillQueue(){ let nextSequence = slides[current_slide].sequence; let iterate = current_slide; let planedSlides = []; let newSlides = []; // Fill planedSlides array for(let a = 1; a < current_slide; a++){ if(slides[a - 1].sequence == nextSequence && slides[a - 1].plan == true){ console.log('a: ' + a) planedSlides.push(a); } } // Fill newSlides array while(slides[iterate].sequence == nextSequence){ iterate++ newSlides.push(iterate) } // Finally fill queue queue = planedSlides.concat(newSlides); }; } // goToSlide function test function goToSlide(slide){ console.log(slide); current_slide = slide; //let player = GetPlayer(); //player.SetVar("goToSlide",slide); }; // Tests setTimeout(() => progress(1), 2000); // at slide intro setTimeout(() => progress(), 4000); // executed at end of slide 1 setTimeout(() => progress(), 6000); // executed at end of slide 2 setTimeout(() => progress(), 8000); // executed at end of slide 3 setTimeout(() => progress(), 10000); // executed at end of slide 4 setTimeout(() => progress(), 12000); // executed at end of slide 5 setTimeout(() => progress(), 14000); // executed at end of slide 2 setTimeout(() => progress(), 16000); // executed at end of slide 6 setTimeout(() => progress(), 18000); // executed at end of slide 7 .. This throws the error
I think, iterate in the following while loop is going out of bounds as iterate starts from slides array index 1:我认为,当迭代从幻灯片数组索引 1 开始时,在以下 while 循环中进行迭代会越界:
// Fill newSlides array
while(slides[iterate].sequence == nextSequence){
iterate++;
newSlides.push(iterate);
}
I would have a console.log statement before and inside while statement.我会在 while 语句之前和内部有一个 console.log 语句。
Modify while loop修改while循环
while(iterate < slides.length && slides[iterate].sequence == nextSequence){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.