Ich versuche, einen Benutzer mit jeder Anfrage zu identifizieren. Gerade jetzt ist dies meine routes
Datei:AngularJs überprüfen Benutzer mit jeder Anfrage
(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('/');
});
}
})();
Die function run
verwendet die authenticationFactory
die wie folgt aussieht:
(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;
}
})()
Aber jetzt erhalte ich den Fehler in meiner Konsole:
routes.js:38 Uncaught TypeError: Cannot read property 'then' of undefined
Was mache ich falsch?
--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' gibt das Anfrageversprechen nicht zurück –
Selbst wenn ich das tue, erhalte ich den gleichen Fehler! Siehe meine Bearbeitung. – Jamie
Bitte beachten Sie die Dokumentation über [$ q] (https://docs.angularjs.org/api/ng/service/$q) Sie benötigen dies, um eine provision (von 'factory.validateUser') zurückzugeben, die Sie beheben/nach dem Validierungsprozess manuell ablehnen –