简体   繁体   English

类型错误:无法读取未定义的属性“then”

[英]TypeError: Cannot read property 'then' of undefined

loginService.islogged() 

Above function return a string like "failed".上面的 function 返回一个类似“failed”的字符串。 However, when I try to run then function on it, it will return error of但是,当我尝试在其上运行 then function 时,它将返回错误

TypeError: Cannot read property 'then' of undefined

and the cursor is indicate right after connected and before .then .并且 cursor 在connected之后和.then之前指示。

Below is the full function:以下是完整的 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

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.我确信$checkSessionServer将导致“失败”字符串。 Nothing more.而已。

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

If you are calling a Python service, the code will look like below: 如果您正在调用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 
}

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. 我们使用MongoDB作为数据库(我知道这没关系。但是,如果有人使用MongoDB + Python(Django)+ AngularJS搜索,结果应该会出现。

Return the promise value inside the function返回函数内的promise值

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

Hi we were getting the same error in the test env we are using the typescript, testing-library/react嗨,我们在使用 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.我使用的是 node v 14.X,但其他人使用的是 node 16.X 及更高版本,他们收到了这个错误。 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然后在测试文件中,在行为中添加了模拟调用,因为它也会影响更新 state

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

); ); }); });

You need to return promise in the function. it suppose to be like:您需要在 function 中返回 promise。它应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM