1

I am using angualrjs and when i launch my app, i am getting the error :

TypeError: Cannot read property 'push' of undefined

Here is the my code :

   app.config([ '$routeProvider','$locationProvider', '$httpProvider', function($routeProvider, $httpProvider,$locationProvider) {


    $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
            return {
                'responseError': function(rejection) {
                    var status = rejection.status;
                    var config = rejection.config;
                    var method = config.method;
                    var url = config.url;

                    if (status == 401) {
                        $location.path( "/login" );
                    } else {
                        $rootScope.error = method + " on " + url + " failed with status " + status;
                    }

                    return $q.reject(rejection);
                }
            };
        });


    $httpProvider.interceptors.push(function ($q, $rootScope, $location) {
        return {
            'request': function(config) {
                if (angular.isDefined($rootScope.authToken)) {
                    var authToken = $rootScope.authToken;
                        config.headers['X-Auth-Token'] = authToken;
                    }
                return config || $q.when(config);
            }
        };
    }
);

}]);

I don't understand why i have this error

3
  • 1
    It means it cannot resolve $httpProvider.interceptors. That is, at the time of execution, $httpProvider.interceptors is probably null, or undefined. Is this script executing at the right time (after angular is loaded)? Commented Nov 7, 2014 at 22:26
  • What to do to control at what time to execute this code? Commented Nov 7, 2014 at 22:32
  • You both helped me. But i can juts accept one answer. Yes,now i remember why the order of parameter is important ( concerning minification) Commented Nov 7, 2014 at 22:39

2 Answers 2

3

The order of your injections is wrong. You put the $locationProvider before the $httpProvider in the array, so you need the same order within the parameters:

app.config([ '$routeProvider','$locationProvider', '$httpProvider', 
function($routeProvider, $locationProvider, $httpProvider) {});
Sign up to request clarification or add additional context in comments.

Comments

3

You are probably injecting wrong object instead of $httpProvider. Your configuration should look like this:

app.config(["$httpProvider", function ($httpProvider) {
    $httpProvider.interceptors.push('myInterceptor');
}]);

UPD: You have a typo, just change your

app.config([ '$routeProvider', '$locationProvider', '$httpProvider', function($routeProvider, $httpProvider, $locationProvider)

to

app.config([ '$routeProvider', '$locationProvider', '$httpProvider', function($routeProvider, $locationProvider, $httpProvider)

(just swap $httpProvider and $locationProvider).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.