How to make controller wait for service to return a value and how to access the returned value in controller?

Ashutosh Pradhan picture Ashutosh Pradhan · Feb 17, 2015 · Viewed 9.2k times · Source

I am new to angularjs. I am having trouble in accessing value that is returned from angularjs service in its controller. Following is the code in controller:

'use strict';
app.controller('AdminTaskCtrl', function ($scope, Search) {
    $scope.buildEnquiry = function (collegeId, ) {
        Search.getIdByEmail(collegeId).then ( function ( result ) {
            $scope.uId = result;
            console.log($scope.uId);
        });
    };
});//controller ends here

And the code in Search service is as follows:

'use strict';
           app.factory('Search',function ($firebase, FIREBASE_URL, $rootScope) {
                          var simpleuser = "";
                          getIdByEmail: function(counsellorEmail) {
                          var collegeuserArray = ($firebase(new Firebase(FIREBASE_URL+"abc/def/")).$asArray());
                          collegeuserArray.$loaded(function(collegeuserArray) {
                              for(var i=0; i<collegeuserArray.length; i++) 
                              {
                                if((collegeuserArray[i].$value) == counsellorEmail)
                                {
                                     simpleuser = collegeuserArray.$keyAt(collegeuserArray[i]);
                                     console.log(simpleuser);
                                     return simpleuser;
                                }
                              }
                          }, function(error) {
                              console.error("Error:", error);
                          });
                    }
                  };     
            );//service ends here.

When the code executes it gives error for .then function as fallows:

TypeError: undefined is not a function and value in controller is not accessed.

Please help.

Answer

Deblaton Jean-Philippe picture Deblaton Jean-Philippe · Feb 17, 2015
'use strict';
       app.factory('Search',function ($firebase, FIREBASE_URL, $rootScope, $q) {
                      var simpleuser = "";
                      getIdByEmail: function(counsellorEmail) {
                      var deferred = $q.defer();
                      var collegeUserArray = ($firebase(new Firebase(FIREBASE_URL+"abc/def/")).$asArray());
                      collegeUserArray.$loaded(function(collegeUserArray) {
                          for(var i=0; i<collegeUserArray.length; i++) 
                          {
                            if((collegeUserArray[i].$value) == counsellorEmail)
                            {
                                 simpleUser = collegeUserArray.$keyAt(collegeUserArray[i]);
                                 console.log(simpleUser);
                                 //return simpleUser;
                                 deferred.resolve(simpleUser);
                            }
                          }
                      }, function(error) {
                          console.error("Error:", error);
                          deferred.reject(error);
                      });
                      return deferred.promise;
                }
              };     
        );

And your controller

'use strict';
app.controller('AdminTaskCtrl', function ($scope, Search) {
  $scope.buildEnquiry = function (collegeId, ) {
    Search.getIdByEmail(collegeId).then ( function ( result ) {
        $scope.uId = result;
        console.log($scope.uId);
    }, function(error){
        //If an error happened, handle it here
    });
  };
});