Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS error .success is not a function

Tags:

angularjs

I have build a factory to handle the functions for my controller, but somehow the controller returns an error on one of the functions:

Error: Auth.getUser(...).success is not a function @http://localhost:8080/app/controllers/mainCtrl.js:10:1
...

I have no idea what's going on here, the rest of the functions seem to be working fine?

main controller:

angular.module('mainCtrl', [])
.controller('mainController', function($rootScope, $location, Auth) {
    var vm = this;
    vm.loggedIn = Auth.isLoggedIn();
    $rootScope.$on('$routeChangeStart', function() {
        vm.loggedIn = Auth.isLoggedIn();
        Auth.getUser()
            .success(function(data) {
                vm.user = data;
            });
    });
    vm.doLogin = function() {
        Auth.login(vm.loginData.username, vm.loginData.password)
            .success(function(data) {
                $location.path('/users');
            });
    };
});
like image 295
Miha Šušteršič Avatar asked Nov 04 '15 20:11

Miha Šušteršič


2 Answers

See 'Deprecation Notice' from $http service documentation:

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead.

You can learn more about these methods in the documentation about $q.

like image 55
avivr Avatar answered Nov 13 '22 00:11

avivr


Here it is the code

Depricated code

$http(  
{  
    method: 'POST',  
    url: '/Home/CreateCustomer',   /*You URL to post*/
    data: $scope.cust   /*You data object/class to post*/
}).success(function (data, status, headers, config)  
{  

}).error(function (data, status, headers, config)  
{  

}); 

Allowed code Angular $http docs

$http(
    {
       method: 'POST',
       url: '/Home/CreateCustomer',  /*You URL to post*/
       data: $scope.cust  /*You data object/class to post*/
    }).then(function successCallback(response) {
       // this callback will be called asynchronously
       // when the response is available


    }, function errorCallback(response) {
       // called asynchronously if an error occurs
       // or server returns response with an error status.

});
like image 21
Friend Avatar answered Nov 13 '22 00:11

Friend