0

Here is the deal i want to add auth token and idx to every http request.
when i try i get a TypeError: Cannot set property 'site' of undefined
here is the interceptor

$httpProvider.interceptors.push(function($q, $cookies,$location) {
  return {
    'request': function(config) {   
      config.data.auth ='hasAuth';
      config.data.token = $cookies.token;
      config.data.idx = $cookies.idx;
      console.log(config);

      return config;
    }

  };
});

And here is the $http request.

app.controller('coolCtrl', function($scope, $http, makeUrl, $cookies){
    var info={};
    info = $cookies.eidx;

    var test = $http.post("https://example.com/uri/list", info )
    .success(function (data) {
        $scope.data = data
    });

});

And the error
TypeError: Cannot set property 'auth' of undefined

I feel like im getting mixed up in all the promises.
Thanks in advance
-James

James Harrington
  • 3,138
  • 30
  • 32

1 Answers1

0

After running a lot of debugs i finally figured it out.

$httpProvider.interceptors.push(function($q, $cookies,$location) {
return {
    'request': function(config) { 
      if (typeof config.data !== undefined){
        config.data.auth ='hasAuth';
        config.data.token = $cookies.token;
        config.data.idx = $cookies.idx;
        console.log(config);
      }
      return config;
    }

  };
});

angular was trying to set config.data.auth when it was making any request including dataless get requests. so i have to check manually for data and then set it.

James Harrington
  • 3,138
  • 30
  • 32