loginService.islogged()
上面的函数返回一个类似“failed”的字符串。但是,当我尝试在其上运行then函数时,它将返回
TypeError: Cannot read property 'then' of undefined
并且光标指示在connected
之后和之前。然后
。
下面是完整的功能:
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
将导致一个“failed”字符串。没别的了。
您需要将您的承诺返回给调用函数。
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
}
TypeError:使用AngularJS调用Django服务时,无法读取未定义的属性“then”。
如果您正在调用一个Python服务,那么代码将如下所示:
this.updateTalentSupplier=function(supplierObj){
var promise = $http({
method: 'POST',
url: bbConfig.BWS+'updateTalentSupplier/',
data:supplierObj,
withCredentials: false,
contentType:'application/json',
dataType:'json'
});
return promise; //Promise is returned
}
我们使用MongoDB作为数据库(我知道这无关紧要。但如果有人使用MongoDB+Python(Django)+AngularJS进行搜索,结果就会出现。