Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Factory - TypeError: Cannot read property 'getSpec' of undefined

I am starting with AngularJS and i am having some issues when trying to use a factory from a controller.

I have the following factory

angular.module('testingApp')
  .factory('factoryService', function ($http) {
    // Service logic
    var getSpec = function(p) {
      return $http.get('http://someurl//?p=' + p);
    };
    return {
      getSpec: getSpec  
    };
  });

and then i try to consume it from the controller as follows

angular.module('testingApp')
  .controller('ServiceincientsCtrl',[ function (factoryService,$scope) {
   console.log('Starting Service Incident Controller');
    factoryService.getSpec('AAA').then(function(response){
        $scope.result = response.data;
    }, function(error){
        console.log('opsssss' + error);
    });

  }]);

But when i try to run it i receive the following message

TypeError: Cannot read property 'getSpec' of undefined

I don't know what i am missing,It should be a newbbie error, I googled it and i tried many examples with the same result.

Any ideas of what i am doing wrong?

Thanks!

like image 328
federom Avatar asked Oct 09 '14 15:10

federom


People also ask

How to fix cannot read property ‘…’ of undefined with angular?

We use arr && arr.length > 0 to make sure arr is defined and it has length bigger than 0. To fix Error: TypeError: Cannot read property ‘…’ of undefined with Angular, we should make sure the variable we’re rendering is defined.

What does TypeError cannot read properties of undefined mean?

TypeError: Cannot read properties of undefined (reading x) Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects.

Why can’t I call a function on an undefined variable?

In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.

What is the meaning of undefined in JavaScript?

Undefined means that a variable has been declared but has not been assigned a value. In JavaScript, properties and functions can only belong to objects. Since undefined is not an object type, calling a function or a property on such a variable causes the TypeError: Cannot read property of undefined.


2 Answers

Looks like you are not using the dependancy array notation properly. Please refer the below code. Please add 'factoryService' & '$scope' as array items.

.controller('ServiceincientsCtrl', ['factoryService', '$scope', function(factoryService, $scope) {
    console.log('Starting Service Incident Controller');
    factoryService.getSpec('AAA').then(function(response) {
        $scope.result = response.data;
    }, function(error) {
        console.log('opsssss' + error);
    });

}]);

Angular documentaion on dependancy injection.

like image 196
msapkal Avatar answered Sep 22 '22 12:09

msapkal


First of all, you didn't declare your controller properly. It should look like this:

.controller('ServiceincientsCtrl',['$scope', 'factoryService', function($scope, factoryService) {

I personally use Services as I find them more readable.

Here's what your factory would look like as a Service:

myApp.service('factoryService', function ($http) {

    this.getSpec = function(p) {
        return $http.get('http://someurl//?p=' + p);
    }

});

This would work with your current controller.

like image 21
Patrick Reck Avatar answered Sep 18 '22 12:09

Patrick Reck