类型错误:无法读取未定义的属性“then" [英] TypeError: Cannot read property 'then' of undefined

查看:57
本文介绍了类型错误:无法读取未定义的属性“then"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

loginService.islogged() 

上面的函数返回一个类似failed"的字符串.但是,当我尝试在其上运行 then 函数时,它会返回错误

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

光标在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 会产生一个失败"的字符串.仅此而已.

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
}

这篇关于类型错误:无法读取未定义的属性“then"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆