Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot read property 'then' of undefined

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.

like image 986
Ezeewei Avatar asked Jul 16 '14 18:07

Ezeewei


People also ask

How do you resolve TypeError Cannot read property of undefined?

To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.

Can not read the property then of undefined?

TypeError - Cannot read property 'then' of undefined is thrown when the caller is expecting a Promise to be returned and instead receives undefined . Let's consider the above examples. In Example 1, the getTaxAmount(price, taxRate) function, when called, should have returned a Promise that resolves to a value of 12 .

How do I resolve a Promise?

Promise resolve() method: Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.

How do you use then in JavaScript?

The then() method in JavaScript has been defined in the Promise API and is used to deal with asynchronous tasks such as an API call. Previously, callback functions were used instead of this function which made the code difficult to maintain. Syntax: demo().


2 Answers

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 } 
like image 71
TheSharpieOne Avatar answered Sep 18 '22 13:09

TheSharpieOne


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.

like image 39
Haris Np Avatar answered Sep 20 '22 13:09

Haris Np