AngularJS类型错误

时间:2015-09-02 17:58:20

标签: javascript angularjs

我是AngularJS的新手,我正在调试别人的代码。我在Google Chrome中调试时出现以下错误:

  

TypeError:accountService.logIn(...)。success不是函数。

以下是我的一些代码。问题出在accountService.logIn(signupform).success(function (response) {行上的signUp表单中。如果需要更多信息,请告诉我。 这是我的整个控制器。

(function () {
'use strict';

angular
    .module('vidaexpress')
    .controller('accountController', accountController);

accountController.$inject = ['$rootScope', '$state', '$stateParams', 'accountService', 'facebookService', 'toastr'];

function accountController($rootScope, $state, $stateParams, accountService, facebookService, toastr) {
    var vm = this;
    vm.logIn = logIn;
    vm.signUp = signUp;
    vm.signOut = signOut;

    vm.facebookLogin = facebookLogin;
    vm.facebookLogout = facebookLogout;

    vm.recoveryPassword = recoveryPassword;
    vm.resetPassword = resetPassword;
    vm.sendVerifyEmail = sendVerifyEmail;

    function logIn(loginform) {
        vm.signinloading = true;
        accountService.logIn(loginform)
        .then(function (response) {
            if (response && response.error) {
                toastr.error(response.error);
            } else {
                accountService.setUserInfo(response);
                if (!$rootScope.returncust) {
                    window.sessionStorage.setItem('returncust', true);
                }
                vm.isAccountOpen = false;                   
            }
        }, function (error) {
            toastr.error(error.error);
        }).finally(function () {
            vm.signinloading = false;
        });
    }
    function signUp(signupform) {
        vm.signuploading = true;
        accountService.signUp(signupform)
        .then(function (response) {
            if (response && response.error) {
                toastr.error(response.error);
            } else {
                accountService.logIn(signupform).success(function (response) {
                    accountService.setUserInfo(response);
                    logIn();
                }).error(function(error) {
                    toastr.error(error.error);
                });
            }
        },function (error) {
            toastr.error('System Error');
        }).finally(function () {
            vm.signuploading = false;
        });
    }
    function signOut() {
        //Log out API
        //accountService.logOut();
        //Log out Facebook
        var userInfo = accountService.getUserInfo();
        if (userInfo.facebookLogin) {
            facebookLogout();
        }
        //Log out UI
        accountService.setUserInfo(null);
        vm.isAccountOpen = false;
        $state.go('main.index');
    }
    function facebookLogin() {
        facebookService.login();
    }
    function facebookLogout() {
        facebookService.logout();
    }
    function recoveryPassword(email) {
        accountService.recoveryPassword(email).then(function (response) {
            if (response && response.error) {
                toastr.error(response.error);
            } else {
                toastr.success('An email has been sent.');
            }
        }, function () {
            toastr.error('System Error');
        });
    }
    function resetPassword(model) {
        model.customerId = $stateParams.id;
        model.token = $stateParams.token;
        accountService.resetPassword(model).then(function (response) {
            if (response && response.error) {
                toastr.error(response.error);
            } else {
                toastr.success('Your password has been reset.');
            }
        }, function () {
            toastr.error('System Error');
        });
    }
    function sendVerifyEmail() {
        accountService.sendVerifyEmail().then(function (response) {
            if (response && response.error) {
                toastr.error(response.error);
            } else {
                toastr.success('Email has sent');
            }
        }, function () {
            toastr.error('System Error');
        });
    }
}
})();

这是我的account.server.js文件。

(function () {
'use strict';

angular
    .module('vidaexpress')
    .service('accountService', accountService);

accountService.$inject = ['$http', '$window', '$rootScope', 'apiUrl'];

function accountService($http, $window, $rootScope, apiUrl) {
    var baseUrl = apiUrl.account;
    var cookieUser;
    var sessionName = 'userInfo';

    this.logIn = logIn;
    this.logOut = logout;
    this.signUp = signUp;
    this.setUserInfo = setUserInfo;
    this.getUserInfo = getUserInfo;
    this.confirm = confirm;
    this.recoveryPassword = recoveryPassword;
    this.resetPassword = resetPassword;
    this.sendVerifyEmail = sendVerifyEmail;

    function logIn(credential) {
        return $http({
            method: 'POST',
            url: baseUrl + 'token',
            headers:{ 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: function (obj) {
                var str = [];
                for (var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
            data: {
                grant_type: 'password',
                email: credential.email,
                password: credential.password
            }
        }).then(function (response) {
            if (!response) {
                return { error: 'System Error' };
            } else if (response.error) {
                return { error: response.error };
            } else {
                return response.data;
            }
        }, function (error) {
            return { error: 'System Error' };
        });
    }
    function logout() {
        return $http.post(baseUrl + 'logout');
    }
    function signUp(userInfo) {
        return $http.post(baseUrl + 'signup', userInfo);
    }
    function setUserInfo(userInfo) {
        cookieUser = userInfo;
        $rootScope.currentUser = userInfo;
        $window.sessionStorage.setItem(sessionName, JSON.stringify(userInfo));
    }
    function getUserInfo() {
        if (!cookieUser) {
            cookieUser = JSON.parse($window.sessionStorage.getItem(sessionName));
        }
        return cookieUser;
    }
    function confirm(customerId, token) {
        $http.get(baseUrl + 'verifyEmail?id=' + customerId + '&token=' + token);
    }
    function recoveryPassword(email) {
        return $http.get(baseUrl + 'recoveryPassword?email=' + email).then(function (response) {
            return null;
        }, function () {
            return { error: 'System Error.' };
        });
    }
    function resetPassword(model) {
        return $http.post(baseUrl + 'resetPassword', model).then(function () {
            return null;
        }, function(){
            return { error: 'System Error' };
        });
    }
    function sendVerifyEmail() {
        return $http.get(baseUrl + 'sendVerifyEmail').then(function (response) {
            return null;
        }, function () {
            return { error: 'System Error.' };
        });
    }
}
})();

1 个答案:

答案 0 :(得分:1)

您的return模式在服务中的运作方式要求您的控制器使用.then()而不是success()

accountService.logIn(loginform)
    .then(function (response) {