调用服务返回undefined

时间:2016-07-20 14:30:22

标签: javascript angularjs angularjs-directive

我正在创建一个名为ActiveUserProfileService的服务,但是当我在控制器中调用它的函数时,我得到了undefined,我无法找出原因。最奇怪的是,在ActiveUserProfileService服务中,来自UserService的信息通过console.log显示,因此我收到了信息,但是在调用了ActiveUserProfileService之后控制器,它给了我undifened。似乎数据没有传递过来。有人能帮助我吗?

UserService.js:

(function () {
    'use strict';

    angular
        .module('myApp')
        .factory('UserService', UserService);

    UserService.$inject = ['$http'];

    /* @ngInject */
    function UserService($http) {
        var service = {
            getAuthenticatedUser:         getAuthenticatedUser,
            getUserInformation:           getUserInformation

        };
        return service;

        function getUserInformation(idUser) {
            return $http.post('api/user/details', {idUser: idUser});
        }
        function getAuthenticatedUser() {
            return $http.get('api/user');
        }

    }

})();

ActiveUserProfileService.js

(function () {
    'use strict';

    angular
        .module('myApp')
        .factory('ActiveUserProfileService', ActiveUserProfileService);

    ActiveUserProfileService.$inject = ['$http','UserService'];

    /* @ngInject */
    function ActiveUserProfileService($http, UserService) {


        var service = {
            isAccount:            isAccount
        };
        return service;

        ////////////////

        function isAccount(accountName) {
            UserService.getAuthenticatedUser()
                .then(function (response) {
                    var data = response.data;

                    UserService.getUserInformation(data.user.id)
                        .then(function (response) {
                            var userDetails = response.data;
                            console.log("It is");
                            console.log(accountName == userDetails.account_types[0].description_internal);

                            return  accountName == userDetails.account_types[0].description_internal;
                        });
                })
        }

    }

})();

我的控制器:

(function () {
    'use strict';

    angular
        .module('myApp')
        .controller('WizardController', WizardController);

    WizardController.$inject = [
        'UserService',

        'ActiveUserProfileService'

    ];

    /* @ngInject */
    function WizardController(UserService,ActiveUserProfileService) {

        var vm = this;


       console.log("ActiveUserProfileService");
    console.log(ActiveUserProfileService.isAccount("professional")); //is Returning me undefined



    }

})
();

1 个答案:

答案 0 :(得分:3)

重点是,您正在尝试在另一个函数(回调函数)中返回isAccount的值。当你这样做时,你将一个值返回给这个函数,而不是isAccount本身,所以isAccount将不返回任何东西,然后是未定义的。 当您调用异步方法时,isAccount也必须是同步的,

替换

 function isAccount(accountName) {
        UserService.getAuthenticatedUser()
            .then(function (response) {
                var data = response.data;

                UserService.getUserInformation(data.user.id)
                    .then(function (response) {
                        var userDetails = response.data;
                        console.log("It is");
                        console.log(accountName == userDetails.account_types[0].description_internal);

                        return  accountName == userDetails.account_types[0].description_internal;
                    });
            })
    }

 function isAccount(accountName) {
    var deferred = $q.defer();
        UserService.getAuthenticatedUser()
            .then(function (response) {
                var data = response.data;
                //when the user is loaded, then you resolve the promise you has already returned
                UserService.getUserInformation(data.user.id)
                    .then(function (response) {
                        var userDetails = response.data;
                        console.log("It is");
                        console.log(accountName == userDetails.account_types[0].description_internal);
                        deferred.resolve(accountName == userDetails.account_types[0].description_internal);
                        return; //here is not isAccount return, but anonymous inner function 'function (response)', you got it?
                    });
            });
    return deferred.promise; //return as soon as creates the promise
}

您肯定还必须注入$ q服务