0

I am trying to implement promises in angularjs resource.But it is showing TypeError: Cannot read property 'then' of undefined error. AngularJs version :v1.2.2

What i have done is

Controller

LibrariesFactory.getLibraryId({
    "communityId": $scope.communityId
}).$promise.then(function(successResponse) {
    console.log(successResponse);
}, function(errorResponse) {

});

Service

serviceApp.factory('LibrariesFactory', function($resource, AppSettings) {
    return $resource(AppSettings.stub_url + 'v1/communities/:communityId/libraries', {
        communityId: '@communityId'
    }, {
        getLibraryId: {
            method: 'GET',
            isArray: true
        }
    });
});

Any Ideas???

Gopesh
  • 3,882
  • 11
  • 37
  • 52

1 Answers1

0

I got the solution of this problem.I am posting the entire code for future references.

Controller

LibrariesFactory.getLibraryId($scope.communityId).then(function(successResponse) {
    console.log(successResponse);
}, function(errorResponse) {
    // console.log(errorResponse);
});

Service

serviceApp.factory('LibrariesFactory', function($resource, AppSettings, $q) {

    return {

        getLibraryId: function(community_id) {
            console.log(t1);
            var Communities = $resource(AppSettings.stub_url + 'v1/communities/' + community_id + '/libraries', {});
            var deferred = $q.defer();
            var communityObj = new Communities();
            communityObj.$query(null, function(community) {
                console.log(community);
                deferred.resolve(community);
            });
            return deferred.promise;
        }
    };
});

Thanks everyone for the reply.

Gopesh
  • 3,882
  • 11
  • 37
  • 52
  • Your solution creates a second promise, why not use the promise that $resource already creates for you? You are also instantiating a new resource (`new Communities()`) which should not be necessary ;) – Sunil D. Aug 28 '14 at 19:25