I'm attempting to generate an array using data retrieved from an API. However, I continue to encounter an error message stating
cannot read property 'push' of undefined in Javascript
.
Could someone please guide me on how to resolve this issue?
Below is the snippet of code in question:
myService.getData().then(function (res) {
$scope.resoureceGroupsRawData = res.data;
//array already declared...
$scope.resourceGroupItems = new Array();
}, function(error) {
throw error;
}).then(function() {
_.forEach($scope.resoureceGroupsRawData, function(text, idx) {
var item = {};
item.id = idx++;
item.label = text;
//why does it say resourceGroupItems is null?
$scope.resouceGroupItems.push(item);
});
}, function(err) {
});