3

So with a factory I'm making a RESTful GET request using $resource, and I'm not getting the data back.

Initially, I get returned $promise and $resolved: false. $resolved eventually becomes true.

I know the url, and headers work because I tested them in the advanced client chrome extension and it works fine. Data is show and everything.

Here is my factory set up, and below that is what I'm referencing in my controller to store it inside a scope variable.

1
  • I am facing the same issue and none of the suggested answers work for me. Here is what I get : Object { $promise: Object, $resolved: true, 1421589555: true } where the real data is the last entry. Commented Jan 22, 2015 at 13:39

3 Answers 3

3

The $resource returns a two-level object. In your controller, it needs to be handled like so:

$scope.terms = GetValues.get();
$scope.terms.$promise.then(function(data){
     //This is where things that happen upon success go
}, function(data){
     //This is where things that happen upon error go
});

You could also write your Service to return as follows and eliminate the $promise from your controller:

.factory('GetValues', ['$resource', function($resource){
// search term is :text
return $resource(apiURL,   {}, {
    get:{
      method: 'GET',
      isArray:true,
      headers: {
          'x-auth-token': 'xxxxx',
          'x-auth-user':  'xxxxx'
      }
    }
  }).$promise;
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

Thnaks for this one. It let me know that my api call was failing.
And for the status I'm getting a 404
Yes, but the point is that you are getting something back, where before you were not.
Your answer was the first to give me the result I needed from the code, so you get the check. Thanks again!
2

You need to utilize the callbacks from $resource:

GetValues.get({}, function(data) {
    $scope.terms = data;
})

5 Comments

Can you log it and post the output using the method above?
Tried to log to the console in side the method, nothing comes up in the log for it
Updated the answer, believe you need to pass blank params, can you try again?
Just to make sure I'm doing it right, could be on me. I just replace my controller line with this right?
Turns out that just adding the {} to define as a vector did the trick. Thanks for the help
1

The $resource constructor returns a promise to actual call. You have to set up the resolve callback for the promise in your controller.

GetValues.get({}, '', function (getResult) {
    // do something with array
    alert(getResult);
}, function (error) {
    console.error('Error getting values', error);
});

The second function is an error callback for the request.

Also, try registering the method with another name, as it isn't very 'nice' to override the native get that expects an object instead of an array:

return $resource(apiURL,{},{ getValues:{
    method: 'GET',... 

And invoke it using

GetValues.getValues().then(... 

PD: there is no need to explicitly return the .$promise

7 Comments

Thanks GonchuB! I'll give this a shot, and see what happens.
Hey. This method returns f has no method then. I still need to add $promise before then for it to work.
Sorry about that. In order for 'then' to work you need the $promise. You can leave it off by declaring your callbacks in the resource call. I edited my answer to match this (notice the first get). Hope it helps and sorry for the confusion.
Older versions of Angular $resource did not have a .$promise to return, but the newer versions (1.2+) do and you do have to deal with it, whether in your service or in your controller.
No worries man, just happy for the help :) Your answer helped me figure out that the issue has nothing to do with my code after testing it with echo.jsontest.com/key/value/one/two. The service in the api for my app returning the 404 is .Net built, so I'll have to talk to the dev on that side to figure it out.
|

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.