简体   繁体   English

在Angular应用上出现错误:TypeError:无法读取未定义的属性'then'

[英]Getting error on Angular app: TypeError: Cannot read property 'then' of undefined

I am getting that error when I do I post from my controller 我从控制器发帖时收到该错误

  function postDashboardsData (dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type : dataType,
        date_range : {
          from : dateFrom,
          to : dateTo
        }
      }, vm.data));
  }

and here I call that function in the same controller 在这里我在同一控制器中调用该函数

  postDashboardsData('overall', $scope.datepickerConf1.overall, $scope.datepickerConf2.overall)
    .then(function(data) {
      console.log('data>>>', data);
      $scope.overallData = data;
    })

that then above is the one returning undefined 那上面是返回未定义的那个

error again 再次错误

TypeError: Cannot read property 'then' of undefined TypeError:无法读取未定义的属性“ then”

what should I do? 我该怎么办?

You forgot about return in your postDashboardsData: 您忘记了在postDashboardsData中return的信息:

function postDashboardsData (dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    var dashboardsDataPromise = Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type : dataType,
        date_range : {
          from : dateFrom,
          to : dateTo
        }
    }, vm.data));

    return dashboardsDataPromise;
}

Explanation 说明

Api.post() function returns a promise which is an object with then function. Api.post()函数返回一个promise,该promise是带有then函数的对象。 Your postDashboardsData function should return this promise, so return must be placed just before execution of Api.post. 您的postDashboardsData函数应返回此承诺,因此必须在执行Api.post之前放置返回值。

You're missing a return in postDashboardsData ie: 您错过了postDashboardsDatareturn ,即:

function postDashboardsData(dataType, dateFrom, dateTo) {

    $scope[dataType + '_done'] = false;

    return Api.post('rotations/' + vm.data[0]._id + '/dashboard', angular.extend({
        type: dataType,
        date_range: {
            from: dateFrom,
            to: dateTo
        }
    }, vm.data));
}

Then you can use postDashboardsData like so: 然后,您可以像这样使用postDashboardsData

postDashboardsData(dataType, dateFrom, dateTo).then(function(response){
     console.log(response, response.data);
})

暂无
暂无

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

相关问题 TypeError:无法读取角度应用程序中未定义的属性“ 0” - TypeError: Cannot read property '0' of undefined in angular app 错误TypeError:无法读取undefined |的属性“0” Angular 4 - ERROR TypeError: Cannot read property '0' of undefined | Angular 4 Angular:ERROR TypeError:无法读取undefined的属性___ - Angular: ERROR TypeError: Cannot read property ___ of undefined 在我的 Angular 表单中获取“类型错误:无法读取未定义的属性‘错误’” - Getting 'TypeError: Cannot read property 'errors' of undefined' in my Angular Form Webpacked Angular2 应用程序类型错误:无法读取未定义的属性“getOptional” - Webpacked Angular2 app TypeError: Cannot read property 'getOptional' of undefined 角嵌套对象-TypeError:无法读取未定义的属性“ app” - Angular nested objects - TypeError: Cannot read property 'app' of undefined 获取TypeError:无法在谷歌应用脚本中读取未定义的属性'getAs' - getting TypeError: Cannot read property 'getAs' of undefined , in google app script 即时通讯收到错误。 angular.js:13550 TypeError:无法读取未定义的属性'$ invalid' - im getting error. angular.js:13550 TypeError: Cannot read property '$invalid' of undefined 单元测试Angular 4收到错误`TypeError:无法读取未定义的属性'subscribe' - Unit test Angular 4 getting error `TypeError: Cannot read property 'subscribe' of undefined` 我不断收到此错误“ angular.js:TypeError:无法读取未定义的属性'pages'” - I keep getting this error “angular.js:TypeError: Cannot read property 'pages' of undefined”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM