简体   繁体   中英

Cannot read property 'then' of undefined error message in AngularJS

I have the following JavaScript code in my service layer. When this code executes, I get an error saying - Cannot read property 'then' of undefined . I looked at some other similar posts on stackoverflow but still couldn't resolve this issue.

Question : What code changes will correct the above error? May be I am calling something in AngularJS the wrong way but not sure.

      function getPreferenceSet(type) {
        var t = angular.isString(type) ? type : '';
        logger.info('getPreferenceSet: fetching; type: ' + t);

        var route = userPreferenceConstants.endPoints.PREFERENCE + '?type=' + t;

        var allDashboardsDeferred = false;
        if (!allDashboardsDeferred) {
            allDashboardsDeferred = $http.get(route)
                .then(getPreferenceSetComplete)
                .catch(function (message) {
                    exception.catcher('XHR failed for getPreferenceSet')(message);
                });
        }
        return allDashboardsDeferred.promise;

        function getPreferenceSetComplete(response) {
            logger.info('getPreferenceSet: complete');
            return response.data;
        }
    }

The call to $http.get(route).then(...).catch(...) is returning an HttpPromise that resolves when the call completes. The allDashboardsDeferred variable is a promise already and not a deferred so you can return it directly:

return allDashboardsDeferred; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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