TypeError:无法读取属性' length'未定义的。我的数据无法加载

时间:2016-06-29 02:53:54

标签: angularjs json html5 controller

我需要帮助来解决我的问题。我有来自后端的呼叫数据的问题。请告诉我如何从后端获取数据。

  

TypeError:无法读取属性'长度'未定义的

我认为我的控制器存在加载数据的问题。

API

application.factory('API', ['Settings',
    function(Settings)
    {
        var api = {};

        api.projects                  = '/ppr/resource/misbactivecaseJPA';

        api.get = function(endpoint)
        {
            return Settings.host.concat(api[endpoint]);
        };

        return api;
    }

]);

服务

application.factory('Project', ['$resource', 'API',
    function($resource, API)
    {
        return $resource(API.get('projects'), {
            Id: '@id',
        }, {
            delete: {
                method: 'DELETE',
            },
            get: {
                method: 'GET',
            },
            save: {
                method: 'POST',
            },
            query: {
                method: 'GET',
                isArray: true,
            },
            update: {
                method: 'PUT',
            },
        });
    }]);

控制器

application.controller('Property_ProjectController', ['$location', '$scope', '$mdDialog' , '$mdMedia', 'Project',
        function($location, $scope, $mdDialog, $mdMedia, Project)
        {
            $scope.limit = 50;
            $scope.page = 0;
            $scope.projects = [];

            $scope.query = {
                $: '',
            };

            $scope.load = function()
            {
                var options = {};
                options.limit = $scope.limit;
                if (++$scope.page > 1) options.skip = $scope.limit * ($scope.page - 1);
                Project.query(options).$promise
                    .then(function(projects)
                    {
                        if (projects.data.length < 1) return;
                        $scope.projects = $scope.projects.concat(projects.data);
                        $scope.load();
                    });
            };

            $scope.load();

HTML

<tbody>
    <tr ng-repeat="projects in data = (projects | filter:query:regex | orderBy:'name')">
        <td>{{ projects.name }}</td>
        <td>{{ projects.code }}</td>
        <td>{{ projects.description }}</td>
        <td>{{ projects.address }}</td>
        <td>{{ projects.ProjectId }}</td>
        <td><md-button  class="md-icon-button"
                        ng-click="AddProject($event)"
                        aria-label="{{ 'action_edit' | translate }}">
                <md-tooltip>{{ 'action_edit' | translate }}</md-tooltip>
                <md-icon>edit</md-icon>
            </md-button>
        </td>
    </tr>
</tbody>

2 个答案:

答案 0 :(得分:0)

您的控制器中只有一行使用.length属性,即行号20。问题是projects.dataundefined,因此显示错误Cannot read property length of projects.data since it is undefined。因此,projects对象没有属性data,因为projects.data未定义。

答案 1 :(得分:0)

您可以尝试使用成功回调,而不是将“then”附加到承诺:

request({
    url: url,
    method: "GET",
    headers: {
        'Content-Type':'application/json'
    },
    body: {
        'sort_field':'age',
        'page_size':10,
        'page':1   
    }
}, function(err, resp, body){
       if(!err && resp.statusCode == 200) {
           res.send(body);
       }     
})

“ps”应该是一个数组(最初为空),并且应该在调用回调函数时定义。