0

I am writing a factory in angular that uses two sets of data. I need one set of data to equal a variable so I can pass that in another API call. For some reason my variable always returns the whole function instead of just returning the variables. All environment variables are available.

var zones = function(){
            var deferred = $q.defer();
            $resource(ENV.web_api_url + ENV.api_version + '/zones/:zoneId', {}, {
                query: {
                    method: 'GET',
                    cache: false,
                    params: {zoneId: '@zoneId', date: $filter('date')(new Date(), "yyyy-MM-dd")},
                    isArray: true,
                    headers: {
                        "X-Auth-Token": $window.sessionStorage.token
                    }
                }
            }).success(function(data){
                deferred.resolve(data);
            }).error(function(){
                deferred.reject('There was an error')
            });
            return deferred.promise;
        };

2 Answers 2

1

you do not need your own promise to use ng-resource:

var zones = function(){
return $resource(query : {...}).query();
}
Sign up to request clarification or add additional context in comments.

1 Comment

If I do this it returns nothing
0

You define a resource and call ".success()" on the actual resource instead on invoking GET. Here is what you should do:

var res = $resource(ENV.web_api_url + ENV.api_version + '/zones/:zoneId', {}, {
            query: {
                method: 'GET',
                cache: false,
                params: {zoneId: '@zoneId', date: $filter('date')(new               Date(), "yyyy-MM-dd")},
                isArray: true,
                headers: {
                    "X-Auth-Token": $window.sessionStorage.token
                }
            }
        });
        res.get(null, function(data){
            deferred.resolve(data);
        },function(){
            deferred.reject('There was an error')
        });

1 Comment

I just edited the syntax there. You should definitely read that page though: docs.angularjs.org/api/ngResource/service/$resource

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.