简体   繁体   English

无法读取未定义错误的属性

[英]Cannot read property of undefined error

I am trying to use an array to get a specific result but i get the error cannot read property of undefined but when i console log that same property i get my result here is my code : 我正在尝试使用数组来获取特定的结果,但是我得到的错误无法读取未定义的属性,但是当我控制台登录该属性时,我得到的结果是我的代码:

for (var j=0;j<$scope.ftListe.length;j++){
    if(task[j].projet_id==id){
        if(temp!=task[j].NomTache)
            temptache.push(task[j]);
        tempcol.push(lecollaborateur[j]);
    }
    temp=task[j].NomTache;
}
$scope.temptache=temptache;
$scope.tempcol=tempcol;

for(var i=0; i<temptache.length;i++){
    for (var k=0;k<$scope.ftListe.length;i++){
        console.log("l'id de la tache: "+temptache[i].IdTache);
        if(temptache[i].IdTache==$scope.ftListe[k].tacheid){
            temps = temps+$scope.ftListe[k].TempsPasse;
            passe= passe+$scope.ftListe[k].TempsPasse * lecollaborateur[k].CoutParJour;
        }
    }
    tempsAr.push(temps);
    passeAr.push(passe);
    temps=0; passe=0;
}

The property I get the error from is IdTache , what am I doing wrong? 我从中收到错误的属性是IdTache ,我在做什么错?

Please take a look at the following lines: 请看以下几行:

for(var i=0; i<temptache.length;i++){
    for (var k=0;k<$scope.ftListe.length;i++){

Note that in the inner for loop, you increment the i variable ( i++ ) instead of k . 请注意,在内部for循环中,将i变量( i++ )而不是k递增。 So this make i to get increment in the inner loop to an out of bounds offset. 因此,这使i可以在内部循环中获得增量,以达到超出范围的偏移量。

The JS runtime error prevent your script from going into an Infinite loop - So when you stated that "when i console log that same property i get my result" it actually show the results up until the point where you're in the valid range of the temptache array. JS运行时错误阻止您的脚本进入无限循环-因此,当您声明“当我用控制台记录相同的属性时,我得到我的结果”时,它实际上显示结果,直到达到您的有效范围为止temptache数组。

You should fix it to: 您应该将其修复为:

for(var i=0; i<temptache.length;i++){
    for (var k=0;k<$scope.ftListe.length;k++){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM