TypeError: Cannot read property 'then' of undefined

JavascriptAngularjsPromise

Javascript Problem Overview


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

and the cursor is indicate right after connected and before .then.

Below is the full function:

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');
});

UPDATE

Here is the islogged() 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;
	});
}

I am certain that the $checkSessionServer will result in a "failed" string. Nothing more.

Javascript Solutions


Solution 1 - Javascript

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
}

Solution 2 - Javascript

TypeError: Cannot read property 'then' of undefined when calling a Django service using AngularJS.

If you are calling a Python service, the code will look like below:

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 
}

We are using MongoDB as the database(I know it doesn't matter. But if someone is searching with MongoDB + Python (Django) + AngularJS the result should come.

Solution 3 - Javascript

Return the promise value inside the function

return new Promise((done, err) => {
        ......
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEzeeweiView Question on Stackoverflow
Solution 1 - JavascriptTheSharpieOneView Answer on Stackoverflow
Solution 2 - JavascriptHaris NpView Answer on Stackoverflow
Solution 3 - JavascriptVelkumaran A PView Answer on Stackoverflow