Reputation: 21406
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;
}
}
Upvotes: 0
Views: 1405
Reputation: 2963
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;
Upvotes: 1