最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to resolve Angularjs TypeError: Cannot read property 'push' of undefined? - Stack Overflow

programmeradmin6浏览0评论

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

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

Share Improve this question edited Nov 7, 2014 at 22:31 Pracede asked Nov 7, 2014 at 22:20 PracedePracede 4,37116 gold badges66 silver badges114 bronze badges 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)? – krisk Commented Nov 7, 2014 at 22:26
  • What to do to control at what time to execute this code? – Pracede 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) – Pracede Commented Nov 7, 2014 at 22:39
Add a ment  | 

2 Answers 2

Reset to default 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) {});

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).

发布评论

评论列表(0)

  1. 暂无评论