Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - TypeError: XX is not a function

Maybe I'm missing some kind of property, but I'm follow this project and I'm getting this error in my controller.

TypeError: loginService.signin is not a function

This is my controller.js

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
        function ($rootScope, $scope, $http, loginService) {

        $scope.signin = function() {

            console.log("username: " + $scope.username);
            console.log("pass: " + $scope.password);

            var formData = {
                username: $scope.username,
                password: $scope.password
            };

            // ERROR IN THIS LINE
            loginService.signin(formData, function(res) {
                console.log("asdf");
                if (res.type == false) {
                    console.log(res.data);
                } else {
                    $localStorage.token = res.data.token;
                    console.log("Window location /");
                }
            }, function() {
                $rootScope.error = "Error en el logueo del usuario";
            });

            // Setting Token:
            $scope.token = $localStorage.token;
        }

    }])

This is my service.js

'use strict';

angular.module('app-services', [])
.factory('loginService', ['$http', '$localStorage', function ($http, $localStorage) {
    console.log('services - loginService');

    var baseUrl = "http://angular-restful-auth.herokuapp.com";

    function changeUser(user) {
        console.log('1');
        angular.extend(currentUser, user);
    }

    function urlBase64Decode(str) {
        console.log('2');
        var output = str.replace('-', '+').replace('_', '/');
        switch (output.length % 4) {
            case 0:
                break;
            case 2:
                output += '==';
                break;
            case 3:
                output += '=';
                break;
            default:
                throw 'Illegal base64url string!';
        }
        return window.atob(output);
    }

    function getUserFromToken() {
        console.log('3');
        var token = $localStorage.token;
        var user = {};
        if (typeof token !== 'undefined') {
            var encoded = token.split('.')[1];
            user = JSON.parse(urlBase64Decode(encoded));
        }
        return user;
    }

    // Set user token.
    var currentUser = getUserFromToken();

    return {
        save: function(data, success, error) {
            $http.post(baseUrl + '/signin', data).success(success).error(error)
        },
        signin: function(data, success, error) {
            $http.post(baseUrl + '/authenticate', data).success(success).error(error)
        },
        me: function(success, error) {
            $http.get(baseUrl + '/me').success(success).error(error)
        },
        logout: function(success) {
            changeUser({});
            delete $localStorage.token;
            success();
        }
    };
}

]);

My application works until the user press the submit button called signin() from a form. Then I got this two lines in my console with the correct data

>> username: somename
>> pass: somepassword

And after that the error shows up. Can anyone help me in order to pass this signin() function?

like image 823
napstercake Avatar asked Jun 16 '15 19:06

napstercake


People also ask

What is TypeError in angular?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.

Is not a function TypeError?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

Is not a function console error?

To solve the "console. log is not a function" error, make sure to place a semicolon between your console. log call and an immediately invoked function expression and don't define any variables named console in your code.

How do you handle uncaught TypeError?

Another way to handle TypeError is to use the alert function. The alert function will display a message to the user and then stop the execution of the JavaScript code. In the following code example, the alert function is used to display a message when the y variable is not a number.


1 Answers

You are messing with Dependency injection array, that should follow correct order of dependency when you are using it in a function.

Code

angular.module('appcontrollers', []).controller('LoginController', ['$rootScope', '$scope', '$http', '$location', '$localStorage', 'loginService',  
        function ($rootScope, $scope, $http, $location, $localStorage, loginService) {
like image 61
Pankaj Parkar Avatar answered Sep 19 '22 12:09

Pankaj Parkar