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!
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
}
);