首页 > 解决方案 > 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"

我真的不明白,为什么它说它无法解析变量,但它可以打印出来?

标签: javascripthtmlangularjs

解决方案


不幸的是,我无法部署您的代码,请您准备 codepen 或 flickr。

我可以提到的几点如下:而不是使用 console.log("username: " + authInfo.username); 使用:console.log('username : ',authInfo.username) 而不是 JSON.stringify(authInfo.authorities) 使用:angular.toJson(authInfo.authorities,true) 还有 console.log(response) 来查看它返回的内容。


推荐阅读