3

I'm trying to get the response of a request using $resource, for example I have:

angular.module('app').factory('AuthResource', ['$resource', function($resource) {
    return {
        isAuthenticated : function() {
            return $resource('/api/v1/auth/authenticated').query();
        }
    }
}]);

Then in my controller I'm calling this service and doing:

console.log(AuthResource.isAuthenticated());

This doesn't return the actual result, which is simply a single object {'success' : 'true'}.

Instead it returns:

Resource {$resolved: false, $then: function, $get: function, $save: function, $query: function…}
$resolved: true
$then: function (callback, errback) {
success: false
__proto__: Resource

How do I go about getting the actual returned object? I'm not applying this to any models, just using the data to determine some routing.

Thank you!

1

3 Answers 3

3

I set up a test like this:

var status = {};
$httpBackend.expectGET("/api/Accounts/AuthenticationStatus").respond(status);

Then I had an expectation:

expect(actual).toBe(status);

I was getting the following error:

Expected { $resolved : true, $then : Function } to be { }.

After scratching my head for a long time, I finally realized that the object returned by the get() function was never going to be exactly the same object I set up the $httpBackend service to respond with, but that it would return the base object { $resolved : ..., $then : ... } and, when resolved, add the additional fields included with my response object to that object.

Hope that makes better sense than the previous poster.

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

Comments

2

Just Modify you code as below

angular.module('app').factory('AuthResource', ['$resource', function($resource) {
    return {
        isAuthenticated : function() {
            return $resource('/api/v1/auth/authenticated')
        }
    }
}]);

--controller 
AuthResource.isAuthenticated().query(function(data){
console.log(data);
});

When the data is returned from the server then the object is an instance of the resource type and all of the non-GET methods are available with $ prefix

2 Comments

This still returns [Resource, $resolved: true, $then: function]
When the data is returned from the server then the object is an instance of the resource type and all of the non-GET methods are available with $ prefix
0

Can you base a solution on the answer here https://stackoverflow.com/a/11856710/1371408. Something like:

AuthResource.isAuthenticated(
    {}, // params (ie. none)
    function (data) { // success callback
        // do what you want with values returned from successful request, contained in 'data'
    },
    function (error) {
        console.log(error); // Error details
    }
);

Comments

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.