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 を示しており、明らかに結果は Resource オブジェクトです。

ここで何が起こっているのか、私は困惑しています。手がかりはありますか?今後これを回避するにはどうすればよいですか?

4

1 に答える 1

1

promise を入れ子にしたい場合は、毎回 promise を返す必要があります。

私の意見では、2番目は不要であり、最初のものは約束を返さないため、最初のものの中で行うことができます。

したがって、次のようになります。

擬似コード:

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

それが機能するかどうか教えてください。

于 2014-03-14T17:39:52.947 に答える