简体   繁体   English

嵌套的承诺返回未定义

[英]Nested promises return undefined

I have a problem with nested promises, inside an angular service I have this method: 我在嵌套服务中有一个问题,在角度服务中,我有以下方法:

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);
        });
    };

Inside a controller I'd like call this method in this manner: 在控制器内部,我想以这种方式调用此方法:

$dataProvider.get(MATCH_ID, TEAM_ID)
        .then(function (result) {
            $scope.teamsheet = result;
            $scope.masterCopy = JSON.parse(JSON.stringify(result));

        });

But I retrieve always the following error: 但是我总是检索以下错误:

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)

What I am doing wrong? 我做错了什么?

if this.get is your get function on $dataProvider then try this code: 如果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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM