首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当所有的文件都被处理完后,我得到了未定义错误的id

当所有的文件都被处理完后,我得到了未定义错误的id
EN

Stack Overflow用户
提问于 2019-05-16 02:16:29
回答 1查看 26关注 0票数 0

简单地说,beow代码应该获取当前正在运行的文件的进度,该文件在后端得到处理。它做了它应该做的所有事情。但是在最后,当所有的文件都被处理完的时候,这意味着当this.currentlyRunning.length的长度是0之后,我会在控制台的“未定义的索引”中得到一个错误。

代码语言:javascript
复制
let file_id = {
      userFileId: this.currentlyRunning[index].id
    }

代码一次又一次地为currentlyRunning数组中的所有文件调用get progress api,直到进度达到100%。这就是我使用setInterval的原因。假设在当前运行的列表中有两个文件,每隔4000毫秒就会调用get progress方法,并在视图中更新每个文件的进度。在处理完这两个文件之后,在控制台上抛出错误"index of undefined“

代码语言:javascript
复制
checkProgress() {
            let index = 0;
            let repeat = setInterval(() => {
              let numberOfFiles = this.currentlyRunning.length;
              if(numberOfFiles === 0) {
                clearInterval(repeat);
              }
            let file_id = {
              userFileId: this.currentlyRunning[index].id
            }
              this.auth.getProgress(file_id).subscribe((res: any)=>{
                    this.currentlyRunning[index]['progress'] = Math.round(res.percent);
                    let indexOfFileHistory =  this.uploadedFileHistory.findIndex((file: any)=> file.id === this.currentlyRunning[index].id);
                    this.uploadedFileHistory[indexOfFileHistory] = this.currentlyRunning[index];
                    if(this.currentlyRunning[index].progress === 100 || res.status == "badBounceRate") {
                      //the origial array which contains all the files we are just updating its status on 100% individual file object is returned with updated status
                      this.uploadedFileHistory[indexOfFileHistory] = res.data;
                      //removing the file from currently running
                      this.currentlyRunning.splice(index, 1);

                    }
                    //incrementing so that next time it runs for the other file
                    index  = index + 1;
                      if(index >= numberOfFiles) {
                        index = 0;
                      }
                      let running =  this.uploadedFileHistory.find((file: any)=>file.status === "Running");
                      if(!running) {
                        clearInterval(repeat);
                    }
                 },(err)=>{ 
                 //want to run it regardless of the error
                  index  = index + 1;
                    if(index >= numberOfFiles) {
                      index = 0;
                    }
                    let running =  this.uploadedFileHistory.find((file: any)=>file.status === "Running");
                    if(!running) {
                      clearInterval(repeat);
                   }
                });
            }, 4000);


    }
EN

回答 1

Stack Overflow用户

发布于 2019-05-16 03:38:10

也许在此之后:

代码语言:javascript
复制
if(numberOfFiles === 0) {
   clearInterval(repeat);
   return; // <-- stop further processing
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56155406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档