[英]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: 您错过了
postDashboardsData
的return
,即:
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.