无法读取属性'推送'未定义的AngularJS

时间:2016-03-30 16:25:38

标签: javascript angularjs frontend

我的目标是根据从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) {

        });

1 个答案:

答案 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
        });