2016-03-27 4 views
1

私はすべてのリクエストにユーザーを識別しようとしています。 今、これは私のroutesファイルです:AngularJsはすべてのリクエストでユーザーを確認します

(function() { 

angular.module('employeeApp').config(routeModule).run(run); 

routeModule.$inject = ['$routeProvider']; 

function routeModule($routeProvider) 
{ 
    $routeProvider.when('/', { 
     templateUrl: '../views/login.html', 
     controller: 'authenticationController', 
     controllerAs: 'authenticationCtrl' 
    }) 
    .when('/home', { 
     templateUrl: '../views/index.html', 
     controller: 'homeController', 
     controllerAs: 'homeCtrl', 
     resolve: { 
      message: function(authenticationFactory){ 
       return authenticationFactory.validateUser(); 
      } 
     } 
    }) 
    .when('/werknemer/:id', { 
     templateUrl: '../views/employee/employee.html', 
     controller: 'employeeController', 
     controllerAs: 'employeeCtrl' 
    }) 
    .otherwise({ 
     redirectTo: '/' 
    }); 
} 


function run(authenticationFactory) 
{ 
    authenticationFactory.validateUser() 
    .then(function (response) { 
     console.log('validateuser'); 
     constants.firstname = response.data.result.Employee.FirstName; 
     constants.companyid = response.data.result.Employee.CompanyId; 
     constants.employeeid = response.data.result.Employee.EmployeeId; 

    }, function() { 
     $location.path('/'); 
}); 
} 

})(); 

function runは次のようになりますauthenticationFactory使用しています。

(function() 
{ 
    angular.module('employeeApp').factory('authenticationFactory', authenticationFactory); 

    function authenticationFactory($cookieStore,$cookies,requestFactory,$location,GLOBALS,constants) 
    { 
     var factory = {}; 



     factory.validateUser = function() 
     { 
      var vm = this; 

      if($location.path() != '/') 
      { 
       var api_token = factory.getToken(); 

       factory.isValidToken(api_token).then(function(response) { 
        if (response.status != 200) { 
         $location.path('/'); 
        } 
        data = {"api_token": api_token}; 
        requestFactory.post(GLOBALS.url + 'show/employee/' + $cookies.get('employeeid'), data) 
       }); 
      } 
     } 

     return factory; 
    } 
})() 

しかし、今、私は私のコンソールにエラーメッセージが表示されます。

routes.js:38 Uncaught TypeError: Cannot read property 'then' of undefined

私は間違って何をしていますか?句が失敗した場合、解決メカニズムは常に約束が返されることを期待している場合

--EDIT--

factory.validateUser = function() 
     { 
      var vm = this; 

      if($location.path() != '/') 
      { 
       var api_token = factory.getToken(); 

       factory.isValidToken(api_token).then(function(response) { 
        if (response.status != 200) { 
         $location.path('/'); 
        } 
        data = {"api_token": api_token}; 
        return requestFactory.post(GLOBALS.url + 'show/employee/' + $cookies.get('employeeid'), data) 
       }); 
      } 
     } 
+0

'factory.validateUser'は要求の約束を返しません –

+0

私はそれをしても同じエラーを受け取ります!私の編集を参照してください。 – Jamie

+0

[$ q](https://docs.angularjs.org/api/ng/service/$q)に関するドキュメントを参照してください。あなたが解決する/検証する(factory.validateUserから)検証処理後に手動で拒否する –

答えて

0

あなたのvalidateUser機能は何も返しません。

var q = $q.defer(); 
q.resolve(); 
return q.promise; 

サービスに$ qを(AngularJSの約束の履行を)注入することを忘れないでください:他にとして機能するようにif節の後に、この自動解決の約束を、追加

してみてください。

関連する問題