1

Angularjs service which is returning my json data from web service is creating error uncaught syntax error unexpected identifier at return statement.

this is my angularjs service-

        app.factory('GetProjectService', function ($http, $q) {
            return {
               getProject: function(str) {
               $http({
                    method: 'GET',
                    url: 'http://localhost:19342/api/Search/GetAllProjects',
                  headers: { 'Content-Type': 'application/json' }
                     }).then(function(response) {
                        if (typeof response.data === 'object') {
                            return response.data;
                        } else {
                            return $q.reject(response.data);
                        }
                    }, function(response) {
                        return $q.reject(response.data);
                    });
               }
        };

    });

2 Answers 2

1

$q is not used properly in your code. Please refer following code snippet -

  app.factory('GetProjectService', function ($http, $q) {
        return {
           getProject: function(str) {
             var deferred = $q.defer();

             $http({
                  method: 'GET',
                  url: 'http://localhost:19342/api/Search/GetAllProjects',
                headers: { 'Content-Type': 'application/json' }
                   }).then(function(response) {
                      deferred.resolve(response.data);
                  }, function(response) {
                      deferred.reject(response.data);
                  });
             return deferred.promise;
           }
    };

});

But when you are $http, there is no need to use $q as $http returns the promise obj. Here is another way of using $http in services -

  app.factory('GetProjectService', function ($http, $q) {
        return {
           getProject: function(str) {

             return $http({
                  method: 'GET',
                  url: 'http://localhost:19342/api/Search/GetAllProjects',
                headers: { 'Content-Type': 'application/json' }
                   });

           }

    };

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

2 Comments

$q.reject is a valid method of the $q service. It creates a promise that is rejected with a specified value. For more information, see AngularJS $q API Reference - $q.reject
Yes, it returns promise, but the code was written in such a way that getProject function is not returning anything, we should return promise by either ways mentioned above.
0

The getProject was missing a return statement.

app.factory('GetProjectService', function ($http, $q) {
    return {
        getProject: function getProject(str) {
            //RETURN http promise 
            return $http({
                method: 'GET',
                url: 'http://localhost:19342/api/Search/GetAllProjects',
                headers: { 'Content-Type': 'application/json' }
            }).then(function onSuccess(response) {
                if (typeof response.data === 'object') {
                    return response.data;
                } else {
                    return $q.reject(response.data);
                }
            }, function onReject(response) {
                return $q.reject(response.data);
            });

        }
    };

});

There needs to be a return statement at each level of the function hierarchy. The factory needs a return statement, the getProject function needs a return statement, and the onSuccess and onReject functions need either a return or throw statement.

Comments

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.