javascript - Angularjs问题:无法解析变量,但变量存在
问题描述
我正在为大学做一个项目,我有一个网站登录名,我必须实施一些操作。我的问题是在用户登录时维护用户会话;因此,如果我在新选项卡中打开网站,我想使用主选项卡的帐户登录。这是我的 loginController 的 angularjs 代码:
mainAngularModule
.controller('LoginCtrl', ['$scope', '$state', 'AuthFactory',
function ($scope, $state, AuthFactory) {
let ctrl = this;
ctrl.authRequest = {username: 'admin', password: 'password'};
ctrl.doLogin = doLoginFn;
ctrl.authMessage = '';
//check if user already logged
let logSession = localStorage.getItem(("authinfo"));
if(logSession == null){
console.log("logSession null");
}
if(logSession == undefined){
console.log("isundefined");
}
if(logSession != null){
console.log("not null");
console.log("usern: " + logSession.username);
//console.log("authinfo authorities: " + logSession.authorities)
AuthFactory.setJWTAuthInfo(logSession);
$state.go("dashboard.home");
}
console.log("login authinfo: " + localStorage.getItem("authinfo"));
let sessionStorage_transfer = function(event) {
if(!event) { event = window.event; } // ie suq
if(!event.newValue) return; // do nothing if no value to work with
if (event.key === 'getSessionStorage') {
// another tab asked for the sessionStorage -> send it
localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
// the other tab should now have it, so we're done with it.
} else if (event.key === 'sessionStorage' && !sessionStorage.length) {
// another tab sent data <- get it
var data = JSON.parse(event.newValue);
for (var key in data) {
sessionStorage.setItem(key, data[key]);
}
}
};
// listen for changes to localStorage
if(window.addEventListener) {
window.addEventListener("storage", sessionStorage_transfer, false);
} else {
window.attachEvent("onstorage", sessionStorage_transfer);
}
function doLoginFn() {
console.log("doLoginFn");
var requiresLogin = $state.jwtToken;
console.log("requireLogin: " + requiresLogin);
AuthFactory.sendLogin(ctrl.authRequest, successCB, errorCB);
function successCB(response) {
let authInfo = response.data;
console.log("data = " + response.data.all);
let header = response.headers();
authInfo.jwtToken = header['authorization'];
console.log("authInfo", authInfo);
// AuthFactory.user.username = authInfo.username;
// AuthFactory.user.role = authInfo.role;
let debugJWT = true;
//if (debugJWT) {
if (true) {
console.log(authInfo);
console.log("username: " + authInfo.username);
console.log("roles: " + JSON.stringify(authInfo.authorities));
console.log("jwtToken: " + authInfo.jwtToken);
console.log("userType: " + authInfo.userRole);
console.log("ended.");
}
AuthFactory.setJWTAuthInfo(authInfo);
//console.log("authinfoo1234: " + authInfo);
// localStorage.setItem("authinfo",authInfo);
console.log("authorities: " + authInfo.authorities);
$state.go("dashboard.home");
}
function errorCB(response) {
let error = response.data;
if (error && error.status === 401) {
ctrl.authMessage = error.message;
}
else {
console.error(response);
ctrl.authMessage = 'No response from server';
}
}
}
}
]);
我有一个非常奇怪的问题:我正在使用 Intellj,它告诉我行
console.log("roles: " + JSON.stringify(authInfo.authorities));
console.log("userType: " + authInfo.userRole);
但是如果我用 localStorage.setItem 和 localStorage.getItem 注释行,控制台会在输出正确的 userType 和 userRole 上打印;如果我添加这些行,控制台会打印出以下消息:
TypeError columnNumber: 17 fileName: " http://localhost:63342/ISSSR_frontend/app/scripts/service/AuthFactory.js " lineNumber: 59 message: "authInfo.authorities is undefined"
我真的不明白,为什么它说它无法解析变量,但它可以打印出来?
解决方案
不幸的是,我无法部署您的代码,请您准备 codepen 或 flickr。
我可以提到的几点如下:而不是使用 console.log("username: " + authInfo.username); 使用:console.log('username : ',authInfo.username)
而不是 JSON.stringify(authInfo.authorities) 使用:angular.toJson(authInfo.authorities,true)
还有 console.log(response) 来查看它返回的内容。
推荐阅读
- loops - 如何在 Rust 的 for 循环中获取当前元素的索引?
- python - 调用 pool.join 时 Python 多进程线程错误或退出代码
- java - Flyway 没有创建表格
- flutter - Flutter - 尝试使用 Tensorflowlite - FloatEfficientNet
- c# - 如何使用 c# 将 DTO 添加到列表对象
- java - 如何在对象列表中推送值?
- flutter - 如何使用 Flutter 在 AnalogClock() 小部件中显示不同时区的时间?
- react-native - 如何创建一个滚动视图,它在某个特定视图处停止?
- sql-server - 用于了解 SQL Server 中用户权限的简单备用查询
- kubernetes - 在非 AWS Kubernetes 集群上使用 aws-virtual-gpu-device-plugin