1

I have the following code. What I am trying to do is create a user object in a service that can then be passed around to any controller I need preventing me from having to make $http calls in the controllers. I'm not a master of async and I'm pretty sure I'm using these promises and $q object wrong. Basically, there is are two Factories that handle the http call and building the object. They seem to be working fine when logged out. Go down to the Service, the Factories are used and inside the functions scope, the value is logged out perfectly, but when I try to return it to a variable in the Services scope, I am getting the values I need, but they are still wrapped the promise object. How do I properly return and resolve these values with Angular, what am I doing wrong here?

'use strict';
/*
*
*   @ngdoc function
*   @name beaverCreekPortalApp.factory:ApiService
*   @description
* 
* 
*/
angular.module('beaverCreekPortalApp').factory('ApiService', function($http) {

var ApiService = {
    async: function(object) {
        return $http.post(object.url, object.data);
    }
};
return ApiService;
});

/*
*
*   @ngdoc function
*   @name beaverCreekPortalApp.factory:APIfactory
*   @description
* 
* 
*/
angular.module( 'beaverCreekPortalApp' ).factory( 'APIfactory', function( $q, ApiService ){
var task = {
    userData : function( callback ){

        var User = {};

        var sessionData = {
            username : sessionStorage['BCResorts.username'] ? sessionStorage['BCResorts.username'] : null,
            access_token : sessionStorage['BCResorts.tok'] ? sessionStorage['BCResorts.tok'] : null,
            account_type : sessionStorage['BCResorts.accountType'] ? sessionStorage['BCResorts.accountType'] : null
        };

        this.userDetails = (function( callback, User ){
            var params = {
                url : '*************',
                data : {
                    username : sessionData.username,
                    access_token : sessionData.access_token
                }
            };

            var deferred = $q.defer();
            ApiService.async(params).then( function(response){                  
                if( response.data.users ){
                    $.each( response.data.users, function( i, v ){
                        if( v.access_token === sessionData.access_token && v.username === sessionData.username ){
                            $.extend( User, sessionData, v );
                            /*
                                this logs out the data I need exactly
                            */
                            console.log( User );
                            deferred.resolve( v );
                            callback( v );
                        }
                    });
                }
            });

        })( callback, User );
    }
}
});

/*
*
*   @ngdoc function
*   @name beaverCreekPortalApp.service:UserService
*   @description
* 
* 
*/
angular.module('beaverCreekPortalApp')
.service('UserService', [ '$q', 'APIfactory', function UserService( $q, APIfactory ) {

var deferred = $q.defer();
var _loadUser = function( _user ){
    APIfactory.userData( function( data ){
        deferred.resolve( data );
        console.log( data );
        /*
             this logs out the data I want exactly but I need it in the scope of the Service so I can pass it to other controllers
        */
    });
    return deferred.promise;
};

var User = _loadUser();
/*
    this logs out the data I want but it is still wrapped in a promise. d ( $$state : object )
    if I try to access the $$state propery, d.$$state, I get status : 1
    if I try to access the $$state.value propery, where the response is, I get undefined
*/
console.log( User );

return User;
}]);
John Slegers
  • 45,213
  • 22
  • 199
  • 169
Clinton
  • 63
  • 2
  • 7
  • Have you stepped through the ApiService.async(params).then() call? If so, are you seeing anything that you do not expect? – Patrick Motard Sep 11 '15 at 21:22
  • 1
    I don't see any returns in your code... not quite sure how any of this would work without actually returning the promises. You created a deferred object and then never returned it, thus making it useless. – Kevin B Sep 11 '15 at 21:42
  • 1
    Avoid the [deferred antipattern](http://stackoverflow.com/q/23803743/1048572). Especially inside of the `each` loop it won't even seem to be working. – Bergi Sep 11 '15 at 21:44
  • You're logging the promise, not the result of the promise. Use `promise.then(function(res) { console.log(res); })` – Bergi Sep 11 '15 at 21:45

0 Answers0