0

我以前使用嵌套指令遇到过这个问题,但我设法在那里找到了解决方法。我的代码看起来有点像,

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 是一个Resource 对象。

我对这里可能发生的事情感到困惑。有什么线索吗?将来如何避免这种情况?

4

1 回答 1

1

如果你想嵌套 Promise,你需要每次都返回一个 Promise。

在我看来,你的第二个是不必要的,可以在第一个内部完成,因为第一个没有返回任何承诺。

所以它可能是这样的:

伪代码:

API.call('token').then(function(result) {
 ...
 return API.call('displayreceipts');
})
.then(function(result){
  var recieptIds = result.result.openReceipts;
})

让我知道它是否有效。

于 2014-03-14T17:39:52.947 回答