数据的正确定义

时间:2015-07-02 05:34:13

标签: angularjs

我有这个代码,当我运行它时,我得到一个错误“ReferenceError:数据未定义”我已经用几种方式定义了数据,但似乎没有什么是正确的。问题是,当我想到它时,数据是在api中声明的(没有?)..会感激一些帮助..

app.service('StatisticsService',
        ['apiClient', '$q', '$rootScope', '$timeout',
            function (apiClient, $q, $rootScope, $timeout) {

                var self = this;


                self.getStatisticsFromServer = function (data) {

                    var deferred = $q.defer(); //1


                    apiClient.getStatsTopMeeters(data)

                            .then(function (response) { //2

                                console.log('externalApiConnect', response);


                                if (response.data.length === 0 || response.result !== "success") {
                                    // TODO: show error
                                    deferred.reject(response);
                                }

                                var stats = response.data;

                                stats = self.getUserProfiles(stats);

                                deferred.resolve(stats);//3

                            }

                            , function (error) {
                                deferred.reject(error);
                                console.error(error);
                            });

                    return deferred.promise; //4

};




                    self.getUserProfiles = function (stats) {
                        var deferred = $q.defer();

                        var emails = [];

                        for (var i = 0; i < stats.length; i++) {
                            emails.push(stats[i].email);
                        }

                        console.log(emails);

                        var data2 = {
                            token: data.token,
                            profile_emails: emails
                        };

                        apiClient.getUserProfiles(data2).then(function (response2) {

                            console.log('getUserProfiles', (response2));

                            if (response2.data.length === 0 || response2.result !== "success") {
                                // TODO: show error
                                deferred.reject(response2);
                            }

                            var stats = response2.data;

                            deferred.resolve(stats);//3

                        }

                        );
                    };

            }]);

这是一些apiClient页面

 app.service('apiClient', ['$http', '$q','$rootScope',  function ($http, $q, $rootScope) {
            var canceler = $q.defer();

            var request = function (apiMethod, apiResponse) {

                switch (apiMethod) {
                    case 'set_meeting_note':
                    case 'get_meeting_details':
                    case 'get_account_details':
                    case 'get_availability':
                    case 'get_best_time':
                    case 'get_participants_statuses':
                    case 'check_user_verified_send_email':
                    case 'get_participants_statuses':
                    case 'get_user_profiles':
                    case 'set_calendar_settings':
                    case 'sync_external_apis':

                         $rootScope.showLoader = false;
                         break;
                    default:
                         $rootScope.showLoader = true;
                          break;
                }
  var config = {
                method: 'POST',
                url: apiPath + apiMethod,
                data: apiResponse,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                withCredentials: true,
                timeout: canceler.promise
            };
            $http(config).success(function (data) {

                $rootScope.showLoader = false;

                if (data.message === undefined) {
                    deferred.resolve(data);
                } else {
                    deferred.reject(data);
                }

            }).error(function (data, status, headers, config) {
                $rootScope.showLoader = false;
                deferred.reject(data);
            });
            return deferred.promise;
        };

        return {



        getStatsTopMeeters: function (data) {
                        return request('get_stats_top_meeters',data );
                    },
        getUserProfiles: function(data){
                         return request('get_user_profiles',data);
                    },

    }

0 个答案:

没有答案