127
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.

5
  • 4
    You don't have a promise here. Commented Jul 16, 2014 at 18:36
  • what what needs to be called to get a msg - that should return a promise. Commented Jul 16, 2014 at 18:37
  • 1
    If the error is regarding undefined, then islogged() doesn't actually return a value. The definition of islogged() would likely be the source of your issues. Commented Jul 16, 2014 at 18:37
  • It is probably that your islogged method not returning a value in some case. After that I see that you are NOT using islogged as async method so it probably not returning promise at all. Commented Jul 16, 2014 at 18:38
  • @Jonathan Lonowski I just posted the islogged function. Hope that bring more insight. Commented Jul 16, 2014 at 18:41

5 Answers 5

154

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
}
2
  • 4
    This was actually the reason why my factory was not returning the promise. I forgot to do return $http.post. As soon as I added the prefix return to $http, it started working correctly.
    – Devner
    Commented Mar 9, 2016 at 10:36
  • 3
    @Devner thank you, I had forgotten to pass return as well.
    – ESR
    Commented Jan 29, 2018 at 22:24
4

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.

-1

Return the promise value inside the function

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

Hi we were getting the same error in the test env we are using the typescript , testing-library/react

I was using node v 14.X but other person was using node 16.X and above they were getting this error. So I updated my node version then able to replicate it on my local.

Then In test file, added the mockcall in the act, as it is affecting in updating the state also

act(() => {
    mockAxios.post.mockImplementation(
        async () => Promise.resolve(availabilitySuccessMockData)
    );
});
-1

You need to return promise in the function. it suppose to be like:

   return new Promise(async (resolve, reject) => {

    const { data:users } = await axios("https://jsonplaceholder.typicode.com/users/" + Number);
    
    const { data: posts } = await axios("https://jsonplaceholder.typicode.com/posts?id=1" + Number);

    const completeData = [users, posts];

    resolve(completeData);
  }); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.