Search code examples
angularjsionic-frameworkangularjs-scope

ionic: Error: updateUserDetails is not defined


After my last upgrade, I have tried to launch my app and I have many errors during the use in the app.

Here is one of the errors I have after I have updated my project (on login, I have many undefined in several different functions):

Error: updateUserDetails is not defined
$scope.register/<@http://localhost:8100/js/controls/loginCntrls.js:126:21
processQueue@http://localhost:8100/lib/ionic/js/ionic.bundle.js:27879:28
scheduleProcessQueue/<@http://localhost:8100/lib/ionic/js/ionic.bundle.js:27895:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29158:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:28969:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8100/lib/ionic/js/ionic.bundle.js:29263:13
done@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23676:36
completeRequest@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23848:7
requestLoaded@http://localhost:8100/lib/ionic/js/ionic.bundle.js:23789:9

Also, when I'm trying to login with Facebook:

ionic.bundle.js:26794 ReferenceError: facebookConnectPlugin is not defined
    at Object.login (ng-cordova.js:2190)
    at Object.login (facebookService.js:35)
    at Scope.$scope.fbLogin (loginCntrls.js:34)
    at fn (eval at compile (ionic.bundle.js:27638), <anonymous>:4:290)
    at ionic.bundle.js:65427
    at Scope.$eval (ionic.bundle.js:30395)
    at Scope.$apply (ionic.bundle.js:30495)
    at HTMLButtonElement.<anonymous> (ionic.bundle.js:65426)
    at defaultHandlerWrapper (ionic.bundle.js:16787)
    at HTMLButtonElement.eventHandler (ionic.bundle.js:16775)

Here is the updateUserDetails page:

 angular.module('starter.controllers',[])
        .controller('loginCtrl', function($scope, $rootScope,  $state, $fbHelper, $ionicLoading, $uiHelper, $api, $ionicHistory, $parsePush) {

            $scope.user = {
                email: "",
                password: ""
            };

            function handleInputError(emailVal, passwordVal){
                var errorMsg = '';
                if(!emailVal){
                    errorMsg = 'Please insert a valid email address';
                } else if(!passwordVal) {
                    errorMsg = 'Please insert your password';
                }
                if(errorMsg) {
                    $uiHelper.popError("", errorMsg,  function(){});
                }
            }

            function goToFeed(){
                $state.go('app.feeds.latest', {type: "getLatestQuestions", reload: true});
            }

    ...

        })


        .controller('registerCtrl', function ($scope, $rootScope,  $state, $ionicLoading, $uiHelper, $api, $ionicHistory, $parsePush) {
            console.log("init registerCtrl");

            $scope.user = {
                email: "",
                nickname: "",
                password: "",
                verify: "",
                gender: "m"
            };

            $scope.register = function($event){
                $event.target.blur();

                var emailVal = $scope.user.email,
                    nicknameVal = $scope.user.nickname,
                    passwordVal = $scope.user.password,
                    verifyVal = $scope.user.verify,
                    genderVal = $scope.user.gender;

                if(!emailVal || !passwordVal ||  !nicknameVal || !verifyVal || !genderVal || passwordVal!= verifyVal  ){
                    handleInputError(passwordVal, verifyVal);
                    return;
                }

                $ionicLoading.show();
                $api.register(emailVal, nicknameVal, passwordVal, genderVal).then(function(data){
                    if(data && data.error){
                        $uiHelper.popError("", data.error, function(){});
                    } else if(data && data._id){
                        updateUserDetails(data.user); //ERROR IS HERE
                        $ionicHistory.nextViewOptions({disableBack: true});
                        $parsePush.initParseNotifications();
                    }

                });
            }
        });

Solution

  • It appears that you're calling this line

    updateUserDetails(data.user); //Error in this line
    

    in your registerCtrl. That function looks like it's defined in your loginCtrl function (I can't be sure since you omitted a decent portion of code from your example). If this is a function you're trying to call from several controllers, you should look at defining it in a service and injecting that service into the controllers that need it.

    Something like so:

    .factory('User', function($rootScope){
        return {
            updateUserDetails:  function(data){
            $rootScope.$broadcast('user:updated',{userId : data._id, userPicture: data.picture, userName: data.name, gender: data.gender});
        }
    }
    

    You'll need to make sure you have a reference to the user in the service, but hopefully, this gets you started.