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.

362350 次浏览

您需要将承诺返回到调用函数。

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: 在使用 AngularJS 调用 Django 服务时,无法读取未定义的属性“ then”。

如果您调用的是 Python 服务,代码如下:

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
}

我们使用 MongoDB 作为数据库(我知道这并不重要。但是如果有人使用 MongoDB + Python (Django) + AngularJS 进行搜索,结果应该会出现。

返回函数内的承诺值

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

嗨,我们在测试环境中得到了相同的错误 我们使用的是类型脚本,test-library/response

我使用的是节点 v14.X,但其他人使用的是节点16.X 及以上版本 他们得到了这个错误。 因此,我更新了我的节点版本,然后能够在我的本地上复制它。

然后在测试文件中,在操作中添加仿真调用,因为它也会影响状态的更新

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

You need to return promise in the function. 它应该是这样的:

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