0

I am brand new to AngularJS and I'm debugging someone else's code. I am getting the following error when I debug in Google Chrome:

TypeError: accountService.logIn(...).success is not a function.

Here is some of my code. The problem is in the signUp form on the line accountService.logIn(signupform).success(function (response) {. If more info is needed please let me know. Here is my entire controller.

(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');
        });
    }
}
})();

Here is my account.server.js file.

(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.' };
        });
    }
}
})();
5
  • you need to inject accountService service in your controller function Commented Sep 2, 2015 at 17:59
  • What does accoutService.login look like? Commented Sep 2, 2015 at 17:59
  • @tymeJV I've updated my question to include accountService code. Commented Sep 2, 2015 at 18:01
  • Change .success to .then in your controller code. Commented Sep 2, 2015 at 18:02
  • @tymeJV .then works. Thank you so much. Post as your answer and I'll mark it. Commented Sep 2, 2015 at 18:07

1 Answer 1

1

The way your return pattern is working in the service requires your controller to use .then() rather than success()

accountService.logIn(loginform)
    .then(function (response) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.