0

I have created a service to communicate with a server via $http.post.

app.factory("dataService", [
    "$http", "$q", "$timeout", function ($http, $q) {

        function post(url, data) {
            var deferred = $q.defer();
            $http.post(url, data)
            .success(function (result) {
                deferred.resolve(result);
            })
            .error(function (error) {
                deferred.reject(error);
            });
            return deferred.promise;
        }

        return {
            post: post
        };

In the controller I am doing:

dataService.get(someUrl,someData).
  then(function (data) {
     $scope.result = data;
 });

Everything works fine, but if I call this generic "dataService" in some other service, for an example:

 app.factory("userService", ["$http", "$q","dataService", function ($http, $q,dataService) {

        function postUser(user) {
            dataService.post(usersUrl, user);
        }

        return {
            postUser: postUser
        };
    }

and call this "userService" in controller userService.postUser($scope.user).then(){}, I am getting the following error: enter image description here

Can anyone explain me why this is happening?

ruud
  • 210
  • 4
  • 12

4 Answers4

0

Try this :

app.factory("dataService", [
              "$http", "$timeout", function ($http) {

                function post(url, data) {
                  return new Promise(function(resolve, reject) {
                    $http.post(url, data)
                      .success(function (result) {
                        resolve(result)
                      })
                      .error(function (error) {
                        resolve(error)
                      });
                  });
                }
                return {
                  post: post
                };

And

app.factory("userService", ["$http", "$q","dataService", function ($http, $q,dataService) {

              function postUser(user) {
                return dataService.post(usersUrl, user);
              }

              // OR

              function postUser(user,callback) {
                dataService.post(usersUrl, user).then(function(data){callback(data);});
              }

              return {
                postUser: postUser
              };
            }

Hope it helps ;)

More info here

Community
  • 1
  • 1
Emidomenge
  • 1,172
  • 17
  • 26
0

I have found a solution. Just needed to assign resolved promise to variable and then return that variable.

   function postUser(user) {
            var result = dataService.post(usersUrl,user);
            return result;
        };
ruud
  • 210
  • 4
  • 12
  • Or you can just do `return dataService.post(usersUrl, user)`, your original code misses the return statement, so your function "returns" undefined on which you try to call `then`. – htulipe Aug 26 '16 at 08:51
  • Yeah, this is fine too. – ruud Aug 26 '16 at 08:59
0

You don't need to use $q here.There are a few angular services that return a $promise, for example $http,$timeout,$interval. Therefore you should not use $q here because a promise is already returned by the $http service.
Your service should look like this:

.factory('myService', function($http) {
       return {
        postMyData : function(){
        return $http.post(data);
 }

At this point you already have a promise returned by the $http angular service. Now you can just resolve it as you want ( controller ).
For example:

var myPromise = myService.postMyData(data).
        myPromise.then(function(){//success
                  },function(){//error
                  },function(){//inform that action is in progress
        })
Stefan.B
  • 136
  • 1
  • 5
0

If you call userService.postUser($scope.user).then(){} the function in userService should return the promise:

 function postUser(user) {
   return dataService.post(usersUrl, user);
 }
gianlucatursi
  • 670
  • 5
  • 19