It is my first application using AngularJS on the client side and I am doing a dashboard that the user must previously access "logging in."
My problem is not with the login , but with the way to recover the current user who is using the application, and doing it as I am doing I get the error of the title in a file called responsive.js
, what I do not understand.
The error code in particular is the following:
Uncaught TypeError: Cannot read property 'substr' of undefined
at loadPage (responsive.js:32)
at HTMLFormElement.<anonymous> (responsive.js:58)
at HTMLFormElement.dispatch (responsive.js:2)
at HTMLFormElement.h (responsive.js:2)
loadPage @ responsive.js:32
(anonymous) @ responsive.js:58
dispatch @ responsive.js:2
h @ responsive.js:2
In principle I have an angle controller that I call when the route is /dashboard
, and access to this private route is handled in the login based on an access token stored in DB:
controllers.js
app.controller('dashboardCtrl', function ($rootScope, $http,localStorageService) {
if(localStorageService.get('token')==null && localStorageService.get('userId')==null){
$location.url('/');
}
else{
$http.get('/laURLamiservicio'+localStorageService.get('userId'),
{headers:{'accept':'application/json','content-type':'application/json','token': localStorageService.get('token')}})
.then(function(response){
console.log(response);
$rootScope.currentUser = response.data.name;
})
}
});
index.html
<!-- Collect the nav links, forms, and other content for toggling -->
<div ng-controller="userMenuCtrl" class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<!-- Right list with user information and logout button-->
<ul class="nav navbar-nav navbar-right">
<!-- User drodown -->
<li ng-show="isUserActive()" class="dropdown ">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
aria-expanded="false"><i class="fa fa-user" aria-hidden="true"></i> {{currentUser}} <span
class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="">Mi perfil</a></li>
<li role="separator" class="divider"></li>
<li><a href="/logout" >Cerrar sesión <i class="fa fa-sign-out" aria-hidden="true"></i></a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
In this fragment of the file index.html you can see that I use another driver that I use to manage the visibility of the user's name inside a navbar, using the directive ng-show that calls a function "isUserActive ()" which returns true or false depending on whether the user is logged in or not to show or not the current user name.
Basically, this is what I want to achieve, the problem is that it really shows it correctly but when debugging the damn error appears. Why is it?