TypeError:无法读取未定义的属性“then”

新手上路,请多包涵
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 将导致“失败”字符串。而已。

原文由 Ezeewei 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 984
2 个回答

您需要将您的承诺返回给调用函数。

 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
 }

原文由 TheSharpieOne 发布,翻译遵循 CC BY-SA 3.0 许可协议

类型错误:使用 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 进行搜索,结果应该会出现。

原文由 Haris Np 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题