类型错误:无法读取属性“,然后”未定义属性、错误、类型、未定义

2023-09-13 02:51:35 作者:哥的世界,不准你放肆

loginService.islogged() 

以上函数返回一个字符串,如失败。然而,当我尝试它,然后运行功能,它将返回错误

Above function return a string like "failed". However, when I try to run then function on it, it will return error of

TypeError: Cannot read property 'then' of undefined

和光标指示右后连接和前。然后

以下是完整功能:

            var connected=loginService.islogged();
            alert(connected);
            connected.then(function(msg){
                alert("connected value is "+connected);
                alert("msg.data value is "+msg.data);
                if(!msg.data.account_session || loginService.islogged()=="failed") $location.path('/login');
            });

更新

下面是 islogged()函数

islogged:function(){
            var cUid=sessionService.get('uid');
            alert("in loginServce, cuid is "+cUid);
            var $checkSessionServer=$http.post('data/check_session.php?cUid='+cUid);
            $checkSessionServer.then(function(){
                alert("session check returned!");
                console.log("checkSessionServer is "+$checkSessionServer);
                return $checkSessionServer;
            })
        }

我可以肯定,在 $ checkSessionServer 将导致一个失败的字符串。仅此而已。

I am certain that the $checkSessionServer will result in a "failed" string. Nothing more.

推荐答案

您需要将你的诺言返回到调用函数。

You need to return your promise to the calling function.

islogged:function(){
        var cUid=sessionService.get('uid');
        alert("in loginServce, cuid is "+cUid);
        var $checkSessionServer=$http.post('data/check_session.php?cUid='+cUid);
        $checkSessionServer.then(function(){
            alert("session check returned!");
            console.log("checkSessionServer is "+$checkSessionServer);
        });
        return $checkSessionServer; // <-- return your promise to the calling function
    }
 
精彩推荐
图片推荐