我的目标是根据从API获得的数据创建数组。但它会不断向我显示cannot read property 'push' of undefined Javascript
的错误。
有人能指出我的出路吗?
以下是代码:
myService.getData().then(function (res) {
$scope.resoureceGroupsRawData = res.data;
//declare an array already...
$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 it says resourceGroupItems is null?
$scope.resouceGroupItems.push(item);
});
}, function(err) {
});
答案 0 :(得分:1)
myService.getData().then(function (res) {
$scope.resourceGroupsRawData = res.data;
//declare an array already...
$scope.resourceGroupItems = new Array();
}, function(error) {
throw error;
}).then(function() {
_.forEach($scope.resourceGroupsRawData, function(text, idx) {
var item = {};
item.id = idx++;
item.label = text;
//why it says resourceGroupItems is null?
$scope.resourceGroupItems.push(item);
});
}, function(err) {
//TODO need add error page here
});