0

I am trying to resolve a $http get request before I return it but I seem to always get undefined result. I have spent a lot of time researching and trying different methods out and still cannot seem to solve it, can any of you see where I am going wrong?

This is the services.js file

(function() {


angular.module('Home')
    .factory('HomeService', HomeService);

function HomeService($http) {
    return {
        getStatus: getStatus()
    };

        function getStatus($scope) {
            $http({
                method: 'GET',
                url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
        }).then(function successCallback(response) {        


        }, function errorCallback(response) {

        });
        }
    }
}());   

This is the controller where I hope to send the resolved result.

function HomeController(HomeService) {      
    HomeService.getStatus;
    console.log(HomeService.getStatus)
    };  
  • `getStatus()` returns nothing. How would you expect it to return anything other than `undefined`? – Ben May 23 '17 at 15:31
  • Can you please show how you are trying to use this? You likely need to return the promise from getStatus or use $q and where you are trying to get a return implement .then() – Doug E Fresh May 23 '17 at 15:31
  • @BenBeck well I can not put the response in there because its outside the function, and I cannot use a return because it returns the promise and not the result. Thats where im stuck. –  May 23 '17 at 15:33
  • @Jayyf9, return the promise then act upon the promise in your controller. – Ben May 23 '17 at 15:35
  • Hi @DougEFresh I have tried to add a bit more context to the question. –  May 23 '17 at 15:37

3 Answers3

0

This is learning to use promises. Here is a quick way to get what you want. I would look into promises and the $q function which you can also implement but I won't detail here. Angular docs on $q Basically, you return the promise from the service and implement the code to run when the promise returns inside the controller.

(function() {

angular.module('Home')
    .factory('HomeService', HomeService);

function HomeService($http) {
    return {
        getStatus: getStatus
    };

        function getStatus($scope) {
            return $http({
                method: 'GET',
                url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
        }); 
        }
    }
    
    // Your controller should use the service like this
    function HomeController(HomeService) {      
    HomeService.getStatus().then(function(response){
      console.log(response.data);
    
    },function(response){
      console.log('an error occured');
    });
     
    
}());
Doug E Fresh
  • 820
  • 8
  • 22
0

You can return your promise in your service so it can be resolved in your controller:

Service:

function getStatus() {
    var promise = $http({
         method: 'GET',
         url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
    });
    promise.then(function(data) {
        return data.data;
    });
};

Controller:

 HomeService.getStatus().then(function(status) {
     $scope.status = status;
 });
Ben
  • 2,441
  • 1
  • 12
  • 16
0

try this:

(function() {


angular.module('Home').service('HomeService', HomeService);

 function HomeService($http) {
    this.getStatus = function() {
        return $http({
            method: 'GET',
            url: 'http://rubynode-iot.chimera.projects.local/sparkDrivers'
         }).then(function successCallback(response) {        
          }, function errorCallback(response) {
         });
    }
}

}());

and in your controller:

angular.module('yourApp')
  .controller('yourController', ['HomeService',
      function(HomeService){
          function getStatusFromSrv(){
             var status;
             HomeService.getStatus().then(function(response){
               status = response;
               console.log("status:" , status);
             });
          }
      }
])
David
  • 633
  • 8
  • 6