未捕获的TypeError:无法读取属性' company_id'未定义的

时间:2015-02-22 01:33:50

标签: javascript arrays ajax typeerror

我从我的脚本中收到此错误,奇怪的是,它仍然有效。它仍然可以运行并更改值。

Uncaught TypeError: Cannot read property 'company_id' of undefine.

这是我的代码:

for(i=0; i<=getUsersCount; i++){
            var user = null;
            var uCompany = null;
            if(getUsers.data[i].company_id === null) uCompany = "Trego";
            else uCompany = getUsers.data[i].company[0].name;
            user = '<tr><th scope="row" class="u'+i+'">'+getUsers.data[i].user+'</td><td class="u'+i+'">'+getUsers.data[i].email+'</td><td class="u'+i+'">'+getUsers.data[i].first+'</td><td class="u'+i+'">'+getUsers.data[i].last+'</td><td class="u'+i+'">'+uCompany+'</td><td><center><button type="button" id="edit'+getUsers.data[i].id+'" class="btn btn-success" data-toggle="modal" data-target="#eUserModal"><span class="glyphicon glyphicon-pencil" style="color:#FFF" aria-hidden="true"></span></button>  <button type="button" id="delete'+getUsers.data[i].id+'" class="btn btn-danger" data-toggle="modal" data-target="#dUserModal"><span class="glyphicon glyphicon-trash" style="color:#FFF" aria-hidden="true"></span></button></center></td></tr>';
            $('#getUsersTable').append(user);
        }

在我的控制台中出错时,这并不是一件大事。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的循环正在迭代getUsersCount + 1次,因此getUsers.data[getUsersCount + 1]未定义。请尝试以下修复:

for(i=0; i<getUsersCount; i++)