AngularJS: неожиданный неопределенный в цепочечных результатах
Ранее я сталкивался с этой проблемой с помощью вложенных директив, но мне удалось найти обходной путь. У меня есть код, который выглядит как
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.
Я в тупике о том, что здесь может происходить. Есть какие-нибудь подсказки? Как я могу избежать этого в будущем?
1 ответ
Если вы хотите вкладывать обещания, вам нужно каждый раз возвращать обещание.
Ваш второй, тогда, на мой взгляд, не нужен и может быть сделан внутри первого, так как первый не дает никаких обещаний.
Так что это может быть что-то вроде:
Псевдо-код:
API.call('token').then(function(result) {
...
return API.call('displayreceipts');
})
.then(function(result){
var recieptIds = result.result.openReceipts;
})
Дайте мне знать, если это работает.