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

Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
ey dee ey em
  • 7,991
  • 14
  • 65
  • 121

5 Answers5

152

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
}
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
TheSharpieOne
  • 25,646
  • 9
  • 66
  • 78
  • 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 Mar 09 '16 at 10:36
  • 3
    @Devner thank you, I had forgotten to pass `return` as well. – ESR Jan 29 '18 at 22:24
3

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.

0

Return the promise value inside the function

return new Promise((done, err) => {
        ......
})
Velkumaran A P
  • 114
  • 1
  • 4
0

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)
    );
});
klutt
  • 30,332
  • 17
  • 55
  • 95
0

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