【发布时间】:2014-09-07 10:01:20
【问题描述】:
loginService.islogged()
上面的函数返回一个类似“失败”的字符串。 但是,当我尝试对其运行 then 函数时,它会返回错误
TypeError: Cannot read property 'then' of undefined
光标位于connected之后和.then之前。
下面是完整的功能:
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 将导致“失败”字符串。仅此而已。
【问题讨论】:
-
你这里没有
promise。 -
需要调用什么来获得
msg- 这应该返回一个承诺。 -
如果错误是关于
undefined,那么islogged()实际上并不是return一个值。islogged()的定义可能是您问题的根源。 -
在某些情况下,您的 islogged 方法可能没有返回值。之后我看到你没有使用 islogged 作为异步方法,所以它可能根本没有返回承诺。
-
@Jonathan Lonowski 我刚刚发布了 islogged 函数。希望能带来更多见解。
标签: javascript angularjs promise