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 probablynull
, orundefined
. 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
2 Answers
Reset to default 3The 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
).