0

I'm trying to access factory service within controller to obtain correct data.

Related controller code looks like:

myApp.controller('RegistrationController', ['$scope','$routeParams','$rootScope','$location','$filter','$mdDialog','checkAttendee', function($scope, $routeParams, $rootScope, $location, $filter, $mdDialog,checkAttendee){

...

    $scope.addAttendee = function(ev) {
        $mdDialog.show({
            controller: AddDialogCntrl,
            templateUrl: 'views/regForm.tmpl.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose:true,
            controllerAs: 'ctrl',
            fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
            locals: {parent: $scope}
        })
        .then(
            function(response){
                if(angular.isDefined(response)){
                    attendees.push(response);
                    checkAttendee.getAttendeeInfo(response);                     
                }
            },
            function(){
                //no changes
            }
        )
        .catch(
            function(error) {
                console.log('Error: ' + error);
            }
        ) 
    };

and factory service code

myApp.factory('checkAttendee', ['$http', function($http) {

    this.getAttendeeInfo = function(req) {

        return $http.get("/check/attendee/",  {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
            .then(function(response) {
                var data = response.data;
                var status = response.status;
                var statusText = response.statusText;
                var headers = response.headers;
                var config = response.config;

                console.log('Data: ' + data);  
                console.log('Status: ' + status);

                return data;    
            })
            .catch(function(response) {
                console.log('something worng');
            });
    }
}]);

but that combination gives me an error Provider 'checkAttendee' must return a value from $get factory method. when there is a return value.

Any thoughts?

1 Answer 1

2

Option 1

When we work with factories the structure should be:

myApp.factory('checkAttendee', ['$http', function($http) {

var factory = {

       getAttendeeInfo : function () {                
            return $http.get(/**/).then(function(response) {
                  // ..
                   return data;   
            }                         
        }
   }       
    return factory;
}]);

DEMO 1

Option 2

you can change factory to service and everything should work. A.e.:

myApp.service('checkAttendee', ['$http', function($http) {

    this.getAttendeeInfo = function(req) {

        return $http.get("/check/attendee/",  {params:{"firstName":req.firstName, "lastName":req.lastName, "email": req.email, "eventID": req.eventID}})
            .then(function(response) {
                var data = response.data;
                var status = response.status;
                var statusText = response.statusText;
                var headers = response.headers;
                var config = response.config;

                console.log('Data: ' + data);  
                console.log('Status: ' + status);

                return data;    
            })
            .catch(function(response) {
                console.log('something worng');
                throw response;
            });
    }
}]);

DEMO 2


Keep in mind that service extends the factory

Sign up to request clarification or add additional context in comments.

4 Comments

It is important to re-throw errors from` .catch` handlers. Otherwise rejected promises will be converted to fulfilled promises.
Somehow I'm getting {"$$state":{"status":0}} for the return factory but then Status: 200 for the console.log('Status: ' + status); Any thoughts?
@JackTheKnife it looks like Promise output. Resolve it in modal: checkAttendee.getAttendeeInfo(response).then() .....
@MaximShoustin Moved some pieces code around and got it to work. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.