简体   繁体   中英

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?

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 . So this make i to get increment in the inner loop to an out of bounds offset.

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.

You should fix it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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