调用服务返回未定义

Calling service returning undefined

本文关键字:未定义 返回 服务 调用      更新时间:2023-09-26

我正在创建一个名为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

    }
})
();

重点是,您正试图在另一个函数回调中返回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服务以及