Подтвердить что ты не робот

TypeError: Невозможно прочитать свойство 'then' из undefined

loginService.islogged() 

Выше функция возвращает строку типа "failed". Однако, когда я пытаюсь запустить, тогда функция на нем, он вернет ошибку

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 приведет к "неудачной" строке. Ничего больше.

4b9b3361

Ответ 1

Вам нужно вернуть свое обещание вызывающей функции.

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
    }