$timeout包装$http.post返回undefined而不是promise
$timeout wrapping $http.post return undefined instead of promise
我试图提交一个要求用户电子邮件不重复的表单,但我想在POST请求之前制作一个小动画。在$scope.process
函数中,我得到了:
CCD_ 2。
之所以会发生这种情况,是因为$scope.process
在$http.post
完成之前返回,但我如何使process()返回promise而不是undefined?
所以,这就是我目前所拥有的:
//The submit form function
$scope.submitForm = function () {
$('.btn').attr('disable');
if ($scope.form.$valid) {
$scope.process($scope.account)
.catch(function (err) {
if (err.code === 'duplicate') {
// handle error
}
});
return false;
}
};
//This is the one in charge to send the request
$scope.process = function(body) {
// Timeout before http post to wait for animation
$timeout(function() {
return $http.post(postUrl, body).then(function (response) {
// This return a promise if I remove the $timeout
var nextPage = response.data;
}).catch(function (err) {
throw err;
});
}, 300);
// Return undefined due to $timeout
};
提前谢谢。
您得到的是TypeError: Cannot read property 'catch' of undefined
,因为您根本没有从process
函数返回promise。从CCD_ 8函数返回CCD_;应用.then
&CCD_ 10而不是CCD_ 11承诺对象。
通过返回$timeout
服务,内部$http.post
将返回一个数据,从而形成适当的链接机制。
代码
$scope.process = function(body) {
// returned promise from here
return $timeout(function() {
//returned $http promise from here.
return $http.post(postUrl, body).then(function (response) {
// This return a promise if I remove the $timeout
nextPage = response.data;
return nextPage; //return data from here will return data from promise.
}).catch(function (err) {
throw err;
});
}, 300);
};
相关文章:
- 使用promise和mongoose对文档进行排序
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- /undefined在我的404错误日志中多次出现
- 测试Angular Service解决错误回调中的promise
- RequireJS向模块传递配置总是返回undefined
- JSON数组数据返回Undefined
- 为什么会出现错误,“UncaughtTypeError:undefined不是函数”
- 节点协同与生成器和Promise并行流量控制
- $timeout包装$http.post返回undefined而不是promise
- Promise.all() - 如何在不返回 undefined 或 value 的情况下解析()
- Angular js 在传递 promise 时$asyncValidator“Undefined is not a f
- 对返回promise的函数使用.success时,Undefined不是函数
- jQuery Promise is undefined
- 在Nodejs中编写promise函数时,undefined不是函数
- undefined with promise async / await
- yield Promise抛出TypeError: undefined不是一个函数
- jQuery ajax返回undefined而不是promise
- $resource angular returning undefined with $promise
- Promise returns undefined
- Bluebird promise变量:'undefined不是函数;