2

Can't get array by $resource. Can you help me? When I use $http all is well

I have error in console:

TypeError: undefined is not a function
at http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:597:29
at forEach (http://127.0.0.1:9000/bower_components/angular/angular.js:327:18)
at angular.module.provider.$get.Resource.(anonymous function).$http.then.value.$resolved (http://127.0.0.1:9000/bower_components/angular-resource/angular-resource.js:595:19)
at deferred.promise.then.wrappedCallback (http://127.0.0.1:9000/bower_components/angular/angular.js:11616:81)
at http://127.0.0.1:9000/bower_components/angular/angular.js:11702:26
at Scope.$get.Scope.$eval (http://127.0.0.1:9000/bower_components/angular/angular.js:12797:28)
at Scope.$get.Scope.$digest (http://127.0.0.1:9000/bower_components/angular/angular.js:12609:31)
at Scope.$get.Scope.$apply (http://127.0.0.1:9000/bower_components/angular/angular.js:12901:24)
at done (http://127.0.0.1:9000/bower_components/angular/angular.js:8487:45)
at completeRequest (http://127.0.0.1:9000/bower_components/angular/angular.js:8703:7)

I created a factory with method

coeffsResource.factory("CoeffsResources",['$resource', 

function($resource) {
  return $resource('/api/:action',{}, {
    get_all_coeffs: { method:'GET', isArray:false, params: {action: 'getAllRegionCoefficients'} },
    save_all_coeffs: { method:'POST', params: {action: 'storeAllRegionCoefficients'} },
    get_manufacturer: { method: 'GET', isArray:true, params: {action: 'getAllManufacturers'} },
    get_models: { method: 'GET', params: {action: 'getModels'} },
    get_classes: {method: 'GET', params: {action: 'getClassesConfig'} },
    get_regions: {method: 'GET', params: {action: 'getAllRegions'} },
    get_ages_config: {method: 'GET', params: {action: 'getAgesConfig'} },
    get_odometer: {method: 'GET', params: {action: 'getOdometersConfig'} },
    get_tax_config: {method: 'GET', params: {action: 'getTaxConfig'} }
  }, {stripTrailingSlashes: false})
}]);

Include factory in controller

angular.module('etachkaEvaluatorFrontendApp')
  .controller('CoeffCtrl', function($scope,  $http, $resource, $q, CoeffsResources) {

      var coeffsResourcesObject =  new CoeffsResources();
      coeffsResourcesObject.$get_manufacturer().then(function() {

      }, function() {

      })
})
scniro
  • 16,844
  • 8
  • 62
  • 106

2 Answers2

1

Why are you newing up a singleton? AngularJS Factories are not intended to work this way. See the AngularJS service docs for more information

Angular services are:

  1. Lazily instantiated – Angular only instantiates a service when an application component depends on it.

  2. Singletons – Each component dependent on a service gets a reference to the single instance generated by the service factory.

Change your usage in CoeffCtrl to the following... (this also assumes you have correctly loaded the ngResource module at some earlier point in your application)

.controller('CoeffCtrl', function($scope,  $http, $resource, $q, CoeffsResources) {
  CoeffsResources.$get_manufacturer().then(function() {

  }, function() {

  })

For a better understanding on factory behavior I have crafted two simple demos. Note that these are not intended to solve your issues in a copy/paste fashion - but to demonstrate what happens when we new an AngularJS factory.

JSFiddle Link - demo - correct

JSFiddle Link - demo - incorrect - TypeError: undefined is not a function

Community
  • 1
  • 1
scniro
  • 16,844
  • 8
  • 62
  • 106
  • 1
    It doesn't work too... When I look to network in devtools, I get a response... function-handler doesn't work(( And I used "new" in other project, it was good( – Andrew Medvedsky Jun 26 '15 at 15:19
1

I think you need to inject the ngResource dependency.

angular.module('etachkaEvaluatorFrontendApp', ['ngResource'])
Donal
  • 31,121
  • 10
  • 63
  • 72
  • 1
    nice find I overlooked that. I have a feeling there are a few issues with this question and we're identifying each. feedback will be necessary – scniro Jun 25 '15 at 15:34