Error: Typeerror: $http.get(...). Success is not a Function

Asked

Viewed 184 times

0

I’m calling an Amazon service and is giving problem, follow code:

Controller:

app.controller('MainController', ['$scope', 'emails', function($scope, emails) {
emails.success(function(data) {
    $scope.email = data;
});
console.log($scope.email); }]);

Factory:

app.factory('emails', ['$http', function($http) { 
return $http.get('https://s3.amazonaws.com/codecademy-content/courses/ltp4/emails-api/emails.json')
.success(function(data) { 
    return data; 
}) 
.error(function(err) { 
    return err; 
}); }]); 

On the console appears:

Typeerror: $http.get(...). Success is not a Function

What can it be?

2 answers

1

The method get of $http returns a Promise, follows below the correct way to make the call.

Controller

app.controller('MainController', ['$scope', 'emails', function($scope, emails) {
  emails.then(function(data) {
      $scope.email = data;
      console.log($scope.email);
  });

}]);

Factory

app.factory('emails', ['$http', function($http) {
    return $http.get('https://s3.amazonaws.com/codecademy-content/courses/ltp4/emails-api/emails.json').then(function(data) { 
        return data;
    }, function (error) {
        return error;
    }); 
}]); 
  • This way also worked, thank you Hiago. :)

  • ;) arrange Rafaela

1


The function success of $http.get is only functional until version 1.4.3 of AngularJS. For subsequent versions, use the method then to capture the outcome of the promise.

Replacing in your code:

app.controller('MainController', ['$scope', 'emails', function($scope, emails) {
  emails.then(function(data) {
      $scope.email = data;
      console.log($scope.email);
  });
}]);

And in the factory:

app.factory('emails', ['$http', function($http) { 
  return $http.get('https://s3.amazonaws.com/codecademy-content/courses/ltp4/emails-api/emails.json');
}]); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.