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

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

问题描述

loginService.islogged() 

以上函数返回一个字符串,如失败。
然而,当我尝试它,然后运行功能,它将返回错误

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

和光标指示右后连接和前。然后

以下是完整功能:

            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
    }

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

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