Javascript AngularJS:链接结果中意外的未定义
我以前在使用嵌套指令时遇到过这个问题,但我设法找到了解决方法。我的代码看起来有点像Javascript AngularJS:链接结果中意外的未定义,javascript,angularjs,promise,angularjs-resource,angularjs-http,Javascript,Angularjs,Promise,Angularjs Resource,Angularjs Http,我以前在使用嵌套指令时遇到过这个问题,但我设法找到了解决方法。我的代码看起来有点像 var token = API.callGeneric({}, {method: 'kds.getTokenExtended2', params: ['demo', 'demo', '', '', '', '', '', false, '', '']}); //kds. token.$promise .then(function (result) { if (!angular.isUndefined(re
var token = API.callGeneric({}, {method: 'kds.getTokenExtended2', params: ['demo', 'demo', '', '', '', '', '', false, '', '']}); //kds.
token.$promise
.then(function (result) {
if (!angular.isUndefined(result.error)) { // API error
$scope.msg = {iconClass: 'glyphicon-exclamation-sign', txt: 'Looks like there was a problem.'}
if (!APIErr.handle(result.error)) { // handle also returns continueExec flags
return;
}
}
$scope.msg = {iconClass: 'glyphicon-cloud-download', txt: 'almost there…'};
$scope.token = result.result;
console.log('result', result.result);
}, function (error) { // server error
$scope.msg = {iconClass: 'glyphicon-exclamation-sign', txt: 'issues with server, summoning the gods'}
APIErr.handle(error);
})
.then(function (result) {
$scope.msg = {}; // clear the message
// another api call to get bills
return API.callGeneric({}, {method: 'kds.getKitchenDisplayReceipts', params: [$scope.token, new Date().getTime()]});
}, APIErr.handle)
.then(function (result) {
console.log(result); // can see result.result.openReceipts
var receiptIds = result.result.openReceipts; // undefined?
}, APIErr.handle);
显然,API是一个调用API的服务
问题是最后几行,其中console.log(result)显示result.result.openReceipts,显然result是一个资源对象
我对这里可能发生的事情感到困惑。有什么线索吗?我以后怎么才能避免这种情况呢?如果你想做出承诺,你需要每次都回复一个承诺 在我看来,你的第二个是不必要的,可以在第一个里面做,因为第一个没有回报任何承诺 所以可能是这样的: 伪代码:
API.call('token').then(function(result) {
...
return API.call('displayreceipts');
})
.then(function(result){
var recieptIds = result.result.openReceipts;
})
让我知道它是否有效。通常,
。然后用返回值解析(即,在下一个中设置值。然后或完成)如果省略了返回值,JavaScript默认返回未定义的,如果是值,它将返回它,如果是承诺,它将(递归地)打开它。