Mr. Supasheva
Mr. Supasheva

Reputation: 311

TypeError: Cannot read property 'get' of undefined

$http.get('/contactList').
success(function(data){
    console.log('got http get');
}).
error(function(data) {
    $scope.error = true;
    $scope.data = data;
    return 'error name';
});

I have an error with just this section of code. I am trying to use the $http.get function with Angular JS. Am I doing something wrong?

I keep getting an error.

Upvotes: 0

Views: 4403

Answers (2)

Sudhansu Choudhary
Sudhansu Choudhary

Reputation: 3360

As I do not see any controller code, I would have to assume one out of the following could be causing this issue,

  1. You don't have all the dependencies injected in your controller as the commentators and @Plato has pointed out. Check this -> TypeError: Cannot call method 'get' of undefined

  2. You have injected all the dependencies but do not match the order, e.g., what should be ['$scope', '$http', function($scope, $http), you would have mentioned like this ['$scope', '$http', function($http, $scope). When using the array notation for injecting dependencies, the order of the arguments is important. Check this -> AngularJS $http.get returns undefined and $http() is not a function

Upvotes: 1

Plato
Plato

Reputation: 11052

As other commenters said you should look at your controller declaration, you need to 'inject' the $http service:

angular.module('myApp')
.controller('messageController', ['$scope', '$http', function($scope, $http){
  $http.get()
  .success()
  .error();
}]);

Upvotes: 1

Related Questions