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
}