Jamie
Jamie

Reputation: 10886

Angular Js syntax error

I've got the following factory:

(function() {
angular.module('temp')
    .factory('Factory',Factory);

    employeeFactory.$inject = ['$http'];
    function employeeFactory($http) {
        var factory = {};
        var vm = this;

        factory.login = function(email,password) {
            return $http({
                'method': 'POST',
                'url': 'http://domain.dev/api/v1/login',
                'data': $.param({
                    'email': email,
                    'password': password
                }),
                'headers': {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });
        return factory;
    }
})();

I receive the error:

Uncaught SyntaxError: Unexpected token )

The console refers to the last line:

})();

Upvotes: 1

Views: 71

Answers (4)

Jenish Rabadiya
Jenish Rabadiya

Reputation: 6766

You were having missing } just before return factory

(function() {
  angular.module('skindustries')
    .factory('employeeFactory', employeeFactory);

  employeeFactory.$inject = ['$http'];

  function employeeFactory($http) {
    var factory = {};
    var vm = this;

    factory.login = function(email, password) {
      return $http({
        'method': 'POST',
        'url': 'http://domain.dev/api/v1/login',
        'data': $.param({
          'email': email,
          'password': password
        }),
        'headers': {
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      });
    }
    return factory;
  }
})();

Upvotes: 0

Simple-Solution
Simple-Solution

Reputation: 4289

I would replace self invoking function with this to make it more readable.

 (function() {
    angular.module('skindustries').factory('employeeFactory', ['$http', function ($http) {

      function login(email, password) {
        return $http({
            'method': 'POST',
            'url': 'http://domain.dev/api/v1/login',
            'data': $.param({
            'email': email,
            'password': password
          }),
         'headers': {
           'Content-Type': 'application/x-www-form-urlencoded'
         }
       });
     }


    // expose public api
    return {
      login: login
    };
  }])}); 

Here is a very good guide on Angular style guide by John Papa

Upvotes: 0

Mustafa Dwaikat
Mustafa Dwaikat

Reputation: 3692

You missed a }

Try this:

(function() {
    angular.module('skindustries').factory('employeeFactory', employeeFactory);
    employeeFactory.$inject = ['$http'];
    function employeeFactory($http) {
        var factory = {};
        var vm = this;
        factory.login = function(email, password) {
            return $http({
                'method': 'POST',
                'url': 'http://domain.dev/api/v1/login',
                'data': $.param({
                'email': email,
                'password': password
                }),
                'headers': {
                'Content-Type': 'application/x-www-form-urlencoded'
                }
            });
        }   
        return factory;
    }
})();

Upvotes: 1

Quentin
Quentin

Reputation: 943214

Run your code through a code formatter to fix the whitespace. The problem becomes obvious.

(function() {
        angular.module('skindustries')
            .factory('employeeFactory', employeeFactory);

        employeeFactory.$inject = ['$http'];

        function employeeFactory($http) {
            var factory = {};
            var vm = this;

            factory.login = function(email, password) {
                return $http({
                    'method': 'POST',
                    'url': 'http://domain.dev/api/v1/login',
                    'data': $.param({
                        'email': email,
                        'password': password
                    }),
                    'headers': {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                });
                return factory;
            }
        })();

Your last }, which you are attempting to use to close the function expression that started on line 1 is actually closing the employeeFactory function.

You then need another } to close the function expression that started on line 1 before you can have a ) to match the ( that is the very first character of the script.

You probably want to put it before the return factory; statement as it looks like the missing } is the one belonging to the anonymous function you assign to factory.login.

Upvotes: 5

Related Questions