d3bug3r
d3bug3r

Reputation: 2586

Angular interceptors of undefined

My angularjs app causing an error:

TypeError: Cannot read property 'interceptors' of undefined

Following is my setup

    var myApp = angular.module("MyApp",['ngResource','ngRoute','ui.router','ngMessages','ngResource','cfp.loadingBar','duScroll','ngAnimate','satellizer','pascalprecht.translate','ngCookies','oc.lazyLoad','ui.bootstrap','toastr','ngNotify','door3.css','ngPassword'])
        .config(['$stateProvider', '$urlRouterProvider','$controllerProvider', '$compileProvider', '$filterProvider', '$provide','$authProvider','$ocLazyLoadProvider','JS_REQUIRES','$httpProvider',function($stateProvider, $urlRouterProvider,$controllerProvider, $compileProvider, $filterProvider, $provide,$authProvider,$ocLazyLoadProvider,jsRequires,ngNotify,$httpProvider){

$httpProvider.interceptors.push('authInterceptor');

}]);

My interceptor:

angular.module('MyApp')
  .factory('authInterceptor', function($rootScope,$q,$window) {
    return {
     request: function(config){
      config.headers = config.headers || {};
      if($window.localStorage.getItem('token')){
        config.headers.Authorization = 'Bearer ' + $window.localStorage.getItem('token');
      }
      return config;

     },

     response: function(response){
      if(response.status === 401) {
        // user not auth
      }
      return response || $q.when(response);
     }
    };
  })

My angular version is 1.4.4, I had no idea what cause these error.

;

Upvotes: 1

Views: 335

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

you did a simple mistake :)

you missed to put ngNotify as an annotation

add the "ngNotify" to the annotation after ...,'JS_REQUIRES', and you will good to go.

Upvotes: 1

Related Questions