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.
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.
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 .
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.
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().
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 }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With