Returning a resolved resource in a service

I am new to writing services. I am calling a method on my factory that invokes a series of operations.

1) Get data from DB 2) Wait for $ prom to decide and then compare and filter the results 3) return a new filtered list

All I get when I go out of $ scope.new_list is an empty object. Can anyone help me with what I am doing wrong?

  people.factory('uniqueContacts', ['apiResource', function(apiResource) {
    function getDB() {
         return apiResource.query({api_resource:'people'})
  }
  function generateList(newlistdata){
    getDB().$promise.then(function(result){

           newlist = _.difference(newlistdata, result);
         })
          return new_list;
    });
  }
    return{
      buildList: function(newlistdata){
        return generateList(newlistdata);
      }
    }
  }]);


//in my controller
  $scope.new_list = uniqueContacts.buildList(newlistdata);
  console.log($scope.new_list) // undefined

      

+3


source to share


3 answers


Your utility function should return new_list

from success

getDB().$promise.then(function(result){
       newlist = _.difference(newlistdata, result);
       return new_list;
     })
});

      



controller

 uniqueContacts.buildList(newlistdata).then(function(new_list){
   $scope.new_list = new_list;
 });

      

+1


source


Because you are trying to output the asynchronous call synchronously, it does not output anything. Try this instead.

// Factory
people.factory('uniqueContacts', [
  'apiResource',
  function(apiResource) {
    function getDB() {
     return apiResource.query({api_resource:'people'})
    }
    function generateList(newlistdata){
      return getDB().$promise.then(function(result){
        // NOTE: You need to return the data you want the promise to
        //  resolve with.
        return _.difference(newlistdata, result);
      }, function (err) {
        // TODO: Handle error
      ).catch(function (err) {
        // TODO: Handle error that may have happened in the `then`.
        // http://odetocode.com/blogs/scott/archive/2014/04/21/better-error-handling-in-angularjs.aspx
      };
    }
    return {
      // NOTE: There is no need to wrap `generateList` in an anonymous
      //  function when both take the same parameters, you can use the
      //  function definition directly.
      buildList: generateList;
    };
  }
}]);

// Controller
// NOTE: `buildList` returns a promise that is asynchronously 
//  resolved/rejected. So in order to get the data returned you must 
//  use the `then` method.
uniqueContacts.buildList(newlistdata).then(function (list) {
  $scope.newList = list;
  console.log($scope.newList);
});

      



Literature:

0


source


You should wait for a response (promise) to your request

function generateList(newlistdata){
    getDB().$promise.then(function(result){
        newlist = _.difference(newlistdata, result);
        return new_list;
    })
});

      


or if you want to handle the error:

function generateList(newlistdata){
    getDB().$promise.then(function(result){
        newlist = _.difference(newlistdata, result);
        return new_list;
    }, function(error) {
    // TODO something when error
    })
});

      

0


source







All Articles