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)
});
}
}
'factory.validateUser'は要求の約束を返しません –
私はそれをしても同じエラーを受け取ります!私の編集を参照してください。 – Jamie
[$ q](https://docs.angularjs.org/api/ng/service/$q)に関するドキュメントを参照してください。あなたが解決する/検証する(factory.validateUserから)検証処理後に手動で拒否する –