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: [{}]
}
];
Well 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"
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With