[英]Nested promises return undefined
我在嵌套服務中有一個問題,在角度服務中,我有以下方法:
this.get = function (matchId, teamId) {
var _key = matchId + '_' + teamId;
var self = this;
var alivePromise = $apiService.alive();
alivePromise.success(function () {
var getPromise = $apiService.get(matchId, teamId);
getPromise.success(function (response) {
self.clearLocal().then(function () {
return self.pushToLocal({ ots: _key, data: response })
.then(function () {
return self.fetchFromLocal(_key);
});
})
});
getPromise.error(function (response) {
return self.fetchFromLocal(_key);
});
});
alivePromise.error(function () {
return self.fetchFromLocal(_key);
});
};
this.fetchFromLocal = function (key) {
return $indexedDB.openStore('teamsheets', function (store) {
store.find(key);
});
}
this.pushToLocal = function (data) {
return $indexedDB.openStore('teamsheets', function (store) {
store.upsert(data);
});
};
在控制器內部,我想以這種方式調用此方法:
$dataProvider.get(MATCH_ID, TEAM_ID)
.then(function (result) {
$scope.teamsheet = result;
$scope.masterCopy = JSON.parse(JSON.stringify(result));
});
但是我總是檢索以下錯誤:
angular.min.js:107 TypeError: Cannot read property 'then' of undefined
at new <anonymous> (team-sheet-details-controller.js:3)
at Object.e [as invoke] (angular.min.js:39)
at Q.instance (angular.min.js:80)
at L (angular.min.js:61)
at g (angular.min.js:54)
at g (angular.min.js:54)
at angular.min.js:53
at angular.min.js:20
at m.$eval (angular.min.js:132)
at m.$apply (angular.min.js:133)
我做錯了什么?
如果this.get是$ dataProvider上的get函數,請嘗試以下代碼:
this.get = function (matchId, teamId) {
var _key = matchId + '_' + teamId;
var self = this;
return $apiService.alive().then(function () {
return getPromise = $apiService.get(matchId, teamId);
getPromise.success(function (response) {
self.clearLocal().then(function () {
return self.pushToLocal({ ots: _key, data: response })
.then(function () {
return self.fetchFromLocal(_key);
});
})
});
getPromise.error(function (response) {
return self.fetchFromLocal(_key);
});
}, function () {
return self.fetchFromLocal(_key);
});
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.