n4mi
n4mi

Reputation: 21

Javascript undefined error yet console log shows the values

Im trying to run this code with no success. I cant find what is causing this undefined error. Im really confused what am i doing wrong! Please have a look on this one and may you help me.

var inbox_items = function(){
        for (var i = start; i < limit; i++)
        {               
            var inbox = data[i];
            console.log(inbox);
            li = li+'<div class="list-group-item">'+
                    '<div class="row">'+
                        '<div class="col-md-1"><input type="checkbox"/></div>'+
                        '<a href="" style="text-decoration:none">'+
                        '<div class="col-md-5">'+inbox.msg_email+'</div>'+
                        '<div class="col-md-2">'+inbox.msg_subject+'</div>'+
                        '<div class="col-md-1">'+inbox.msgStatus_id+'</div>'+
                        '<div class="col-md-3">'+inbox.msg_date+" "+inbox.msg_time+'</div>'+
                        '</a>'+
                    '</div>'+
                  '</div>';             
        }
        return li;
    }

    switch(action)
    {
        case 'sentItems': $('#list_msgboxlist_msgbox').append(sent_items);                              
                            break;
        default: $('#list_msgbox').append(inbox_items);                     
                    break;
    }

enter image description here

Upvotes: 0

Views: 109

Answers (2)

thanhpk
thanhpk

Reputation: 4297

inbox is undefined because of array data have a undefined element You can just skip the undefined element like this

if(data[i] == undefined) continue;
var inbox = data[i];
console.log(inbox);

Upvotes: 0

Derlin
Derlin

Reputation: 9891

Either the length of data does not match the limit, so you end up with a data[i] of undefined, or you have undefined elements in your data.

Maybe use:

if(!inbox) continue; 

Upvotes: 1

Related Questions