1

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:

})();
0

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

what is the meaning of two return statement inside the factory.login?
Thankyou that helped !
@JenishRabadiya - The first return essentially returns the resulting response from the POST request. whereas the second return; returns|exposes the factory and its associated api's publicly. So in above example, you're exposing the login(u,p) function to other components within your app when this service is injected as dependency in your controller|service etc.
@Simple-Solution No. That's not correct I believe line return factory; would never be executed. Look at the code carefully. return statement has followed by another return statement. I would mark that statement as unreachable code.
1

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

Comments

0

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

Comments

0

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

2 Comments

That will error. JS Hint reports: Expected an identifier and instead saw ')'.
@Quentin Yup I fixed it.

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.