javascript - Uncaught TypeError: Cannot read property 'xxxx' of undefined, But it's defined -


i having trouble problem hours ago. wrote these in jquery function check value in variable if it's exist or not.

console.log("in tr"); console.log(tmproomlistarray[c].room_name); let roomname = tmproomlistarray[c].room_name; 

when ran , looked @ google chrome console. shows results this.

rome  <<< in console.log shows valid value                                    in tr                                                        uncaught typeerror: cannot read property 'room_name' of undefined <<  happen when assign in variable , need  

what happen ?? i'm blind right now..

edit : edit question content

i solved it. silly problem overlooked. it's array index.

i put

tmproomlistarray[c-1] 

into function , worked. think solution rest more, see problem more clearly.

for want know happen read below.

i got function insert row in table, , function have 2 parameters, array. function insert row continuously table. won't stop until reach end of array.

var day = ["1","2","3"]; var room = ["room1","room2","room3",....,"room16"] // 16 elements; var timeforheader = ["08:00","09:00",....."20:00"] // 25 elements;   function addrow(paramday,paramroom){   // function create many rows depends on paramroom length , columns depends on length of time header.    var daylength = day.length;    var roomlength = room.length;    var timeforheaderlength = timeforheader.length;    for(var = 0; < daylength; i++)    {       // rows        for(var c = 0; c < timeforheaderlength+1; c++)        {           // made c +1 timeforheaderlength because want header            //this column           if(c == 0)           {              // if c == 0 mean insert header row element in table              var tr = $("<tr/>");              var td = $("<td/>",{text:timeforheader[0]+" - "+timeforheader[1]}).appendto(tr);              ...              ...              var td23 = $("<td/>",{text:timeforheader[23]+" - "+timeforheader[24]}).appendto(tr);              $("#mytableid").append(tr);           }else{             // row contain room's name , content             // , part cause me problem.             // in else condition c must 1-17 when access tmproomlistarray contains 16 element             // right guys should know right. tmproomlistarray's index must 0 - 16               // worked when c between 1 - 16, didn't work on last c cause c equal 17              console.log(tmproomlistarray[c].room_name);             let roomname = tmproomlistarray[c].room_name;             //it should              let roomname = tmproomlistarray[c-1].room_name;             // hahahahahahahahahahahahahahahahahhaha           }                      }     } } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -