最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs getting value of response using $resource - Stack Overflow

programmeradmin0浏览0评论

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!

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!

Share Improve this question asked Apr 24, 2013 at 16:42 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges 1
  • check out [this answer][1] , it helped me out [1]: stackoverflow./a/16197255/1505895 – paynestrike Commented Jun 28, 2013 at 1:34
Add a ment  | 

3 Answers 3

Reset to default 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.

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

Can you base a solution on the answer here https://stackoverflow./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
    }
);
发布评论

评论列表(0)

  1. 暂无评论