admin管理员组文章数量:1189386
while pushing an element for the first time to a child array which is null,I'm getting this error "Cannot read property push of null" But the element gets pushed,and the second time I do everything goes fine.It gets added to the array
this.group.departmentsList.push({
name: group.newCategoryName,
sortOrder: group.departmentsList.length,
type: "category"
});
group contains the data and departmentList is the child array which is declared like this:
$scope.parentDepartment = [
{
departmentsList: [{}]
}
];
while pushing an element for the first time to a child array which is null,I'm getting this error "Cannot read property push of null" But the element gets pushed,and the second time I do everything goes fine.It gets added to the array
this.group.departmentsList.push({
name: group.newCategoryName,
sortOrder: group.departmentsList.length,
type: "category"
});
group contains the data and departmentList is the child array which is declared like this:
$scope.parentDepartment = [
{
departmentsList: [{}]
}
];
Share
Improve this question
edited Oct 9, 2014 at 7:53
dfsq
193k26 gold badges242 silver badges259 bronze badges
asked Oct 9, 2014 at 7:50
forgottoflyforgottofly
2,71912 gold badges54 silver badges96 bronze badges
1 Answer
Reset to default 25Well don't push to nonexistent array maybe? You can explicitly check if it's not null and create one if needed:
this.group.departmentsList = this.group.departmentsList || [];
this.group.departmentsList.push({
name: group.newCategoryName,
sortOrder: group.departmentsList.length,
type: "category"
});
本文标签: javascriptCannot read property push of nullStack Overflow
版权声明:本文标题:javascript - Cannot read property push of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738387261a2084246.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论