2

Hi I am a total newbie to AngularJS. I'm trying to debug someone else's code and I'm getting an error message that I'm not sure how to fix. The error message is Uncaught ReferenceError: response is not defined. Here is my controller:

(function () {

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

accountManagerController.$inject = ['$state', 'accountManagerService', 'toastr'];

function accountManagerController($state, accountManagerService, toastr) {
    var vm = this;
    vm.updatePassword = updatePassword;
    vm.updatePhone = updatePhone;

    init();

    function init(){
        getCustomerInfo();
    }

    function getCustomerInfo() {
        accountManagerService.getCustomerInfo().then(function (response) {
            vm.customerInfo = response;
        }, function (error) {
            vm.error = error;
        });
    }

    function updatePassword(password) {
        accountManagerService.updatePassword(password).then(function (response) {
            if (response.error) {
                displayError(response.error);
            } else {
                $state.go('main.manage.index');
            }
        }, function (error) {
            displayError(response.error);
        });
    }

    function updatePhone(phone) {
        accountManagerService.updatePhone(phone).then(function (response) {
            if (response.error) {
                displayError(response.error);
            } else {
                $state.go('main.manage.index');
            }
        }, function (error) {
            displayError(response.error);
        });
    }

    function displayError(error) {
        toastr.error(error, 'Error');
    }
}

})();

and this is my service. If any other information is needed please let me know (function () {

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

accountManagerService.$inject = ['$http', 'apiUrl'];

function accountManagerService($http, apiUrl) {
    var baseUrl = apiUrl.account;
    this.updatePassword = updatePassword;
    this.updatePhone = updatePhone;
    this.getCustomerInfo = getCustomerInfo;

    function getCustomerInfo() {
        return $http.get(baseUrl + 'getCustomerInfo').then(function (response) {
            return response.data;
        }, function (error) {
        });
    }

    function updatePassword(password) {
        return $http.post(baseUrl + 'updatePassword', password)
        .then(function (response) {
            return {};
        }, function(error) {
            return { error: error.data.message };
        });

    }

    function updatePhone(phone) {
        return $http.post(baseUrl + 'updatePhone', phone)
        .then(function (reponse) {
            return {};
        }, function (error) {
            return { error: error.data.message };
        });
    }  
}

})();

Here is my view:

form class="form form-vertical" id="new_pass" name="new_pass" ng-submit="new_pass.$valid && vm.updatePassword(vm.password)" novalidate>
<div class="form-group" ng-show="vm.updatePasswordError">
            <span class="alert alert-danger col-lg-12">{{vm.updatePasswordError}}</span>
</div>  
<div class="form-group">
    <div class="row">
        <div class="col-lg-12">
            <label for="currpw" class="control-label">{{'PASSWORD_ENTER_CURRENT' | translate }}</label>
            <input id="currpw" class="form-control" title="New Password" required="" type="password" ng-model="vm.password.oldPassword">
        </div>
    </div>
</div>

    <div class="row">
        <div class="col-lg-12">
            <label for="currpw" class="control-label">{{ 'PASSWORD_ENTER_NEW' | translate }}</label>
            <input id="signuppassword" name="signuppassword" type="password" class="form-control" autocomplete="off" required ng-model="vm.password.newPassword" ng-pattern="/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,18}$/" ng-focus="vm.signuperror = '';">
            <div class="error_msg" ng-show="new_pass.signuppassword.$error.required && submit">{{ 'PASSWORD_ENTER' | translate }}</div>
            <div class="error_msg" ng-show="new_pass.signuppassword.$error.pattern && submit">
                {{ 'PASSWORD_ERROR_PATTERN' | translate }}
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <div class="col-lg-12">
                <label for="newpw" class="control-label">{{ 'PASSWORD_ENTER_NEW_CONFIRM' | translate }}</label>
                <input id="confirmpassword" name="confirmpassword" type="password" class="form-control" required ng-model="vm.password.confirmpassword" ve-compare-to="vm.password.newPassword" ng-focus="vm.signuperror = '';">
                <div class="error_msg" ng-show="new_pass.confirmpassword.$error.required && submit">{{ 'PASSWORD_ENTER_CONFIRM' | translate }}</div>
                <div class="error_msg" ng-show="new_pass.confirmpassword.$error.veCompareTo && submit">{{ 'PASSWORD_ERROR_MATCH' | translate }}</div>
            </div>
        </div>
    </div>

        <div class="row">
            <div class="col-lg-4 col-lg-12 buttonblocks">
                <button type="submit" class="btn btn-primary button_dkblue_lg" ng-click="submit=true;">{{ 'UPDATE' | translate }}</button>
            </div>
    <div class="col-lg-8 col-xs-12 buttonblocks align-right">
        <a ui-sref="main.manage.index"  class="btn btn-primary button_dkblue_lg">{{ 'CANCEL' | translate }}</a>
            </div>
        </div>

<style>
.far_right{float:right;}
.pwalert { margin-top: 29px;}

2
  • Where is the error thrown? Commented Sep 9, 2015 at 15:35
  • @isherwood It is thrown on getCustomerInfo, and on updatePassword. In the controller. Commented Sep 9, 2015 at 15:39

1 Answer 1

3

In your error callbacks, you sometimes use response while you should use error.

Example with updatePassword:

function updatePassword(password) {
    accountManagerService.updatePassword(password).then(function (response) {
        // ...
    }, function (error) {
        // displayError(response.error); <-- this must be your error:
        // there is no response variable in current scope
        displayError(error);
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Well it's not throwing an error anymore, but my code still doesn't work. I'll add my view to the question as well, but what is supposed to happen is that it should check the current password and make sure it matches the users current password. It's not doing it though, it's letting you put anything in as the current password.

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.