Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider 'xx' must return a value from $get factory method

Here is my angular service:

angular
    .module('myApp')
    .factory('partyService', function($http) {
        this.fetch = function() {
            return $http.get('/api/parties')
                        .then(function(response) {
                            return response.data;
                        });
        }
    });

I think this service is returning data (or an empty array // not sure)

Then why am I geeting this error:

Error: [$injector:undef] Provider 'partyService' must return a value from $get factory method.

Update:

If I use service instead of factory then I don't get any errors. Why so???

like image 301
Vishal Avatar asked Aug 10 '16 22:08

Vishal


2 Answers

angular
.module('myApp')
.factory('partyService', function($http) {

    function fetch = function() {
        return $http.get('/api/parties')
                    .then(function(response) {
                        return response.data;
                    });
    }

    function anotherFetch = function() {
        return $http.get('/api/parties')
                 .then(function(response) {
                     return response.data;
                 });
    }

  return {
    fetch,
    anotherFetch,
  }


});
like image 142
D V Yogesh Avatar answered Nov 01 '22 17:11

D V Yogesh


The error speaks for itself. You must return something in your factory:

var factory = {
  fetch: fetch
};

return factory;

function fetch() {
  return..
}

Then, in your controller:

partyService.fetch()...
like image 25
developer033 Avatar answered Nov 01 '22 16:11

developer033