JavaScript
x
2
1
loginService.islogged()
2
Above function return a string like “failed”. However, when I try to run then function on it, it will return error of
JavaScript
1
2
1
TypeError: Cannot read property 'then' of undefined
2
and the cursor is indicate right after connected
and before .then
.
Below is the full function:
JavaScript
1
9
1
var connected=loginService.islogged();
2
alert(connected);
3
connected.then(function(msg){
4
alert("connected value is "+connected);
5
alert("msg.data value is "+msg.data);
6
if(!msg.data.account_session || loginService.islogged()=="failed")
7
$location.path('/login');
8
});
9
UPDATE
Here is the islogged()
function
JavaScript
1
11
11
1
islogged:function(){
2
var cUid=sessionService.get('uid');
3
alert("in loginServce, cuid is "+cUid);
4
var $checkSessionServer=$http.post('data/check_session.php?cUid='+cUid);
5
$checkSessionServer.then(function(){
6
alert("session check returned!");
7
console.log("checkSessionServer is "+$checkSessionServer);
8
return $checkSessionServer;
9
});
10
}
11
I am certain that the $checkSessionServer
will result in a “failed” string. Nothing more.
Advertisement
Answer
You need to return your promise to the calling function.
JavaScript
1
11
11
1
islogged:function(){
2
var cUid=sessionService.get('uid');
3
alert("in loginServce, cuid is "+cUid);
4
var $checkSessionServer=$http.post('data/check_session.php?cUid='+cUid);
5
$checkSessionServer.then(function(){
6
alert("session check returned!");
7
console.log("checkSessionServer is "+$checkSessionServer);
8
});
9
return $checkSessionServer; // <-- return your promise to the calling function
10
}
11