4

I've been attempting to perform a Restangular GET with multiple query parameters (for simple pagination) and it doesn't seem possible--at least with what I'm trying to do. Here's what I'm attempting to do:

Restangular.all('elevation').get({ pageParam: page, pageSizeParam: pageSize }).then(function(data) {
    console.log(data);
});

WIth the expected response looking something like:

{ totalRecords: 233, elevations: {...} }

This does not work and results in the following:

GET http://localhost:24287/elevation/[object%20Object] 404 (Not Found)

I also attempting utilizing customGET as well which results in the same problem as above.

The only way I'm actually able to pass multiple query parameters is by using getList. Unfortunately when utilizing the getList unsurprisingly the following error is thrown:

Error: Response for getList SHOULD be an array and not an object or something else

To resolve this issue, the Restangular documentation states in the My response is actually wrapped with some metadata. How do I get the data in that case? section that I need to use addResponseInterceptor which I've done like so:

 RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
  var newResponse = [];
  if(response.data.totalRecords !== undefined) {
    newResponse.elevationData= {};
    newResponse.elevationData.totalRecords = response.data.totalRecords;
    newResponse.elevationData.elevations = response.data.elevations;
  }
  return newResponse;
});

After jumping through hoops this convoluted solution does in fact work. Is there not an easier way to simply call Restangular's get with multiple query parameters and get an object returned?

At this point, it would be a magnitude easier to take Restangular out of the question and simply use the $http service like so:

$http.get('http://localhost:24287/elevation?page=' + page + '&pageSize=' + pageSize).then(function(result) {
    console.log(result.data.totalRecords);
    console.log(result.data.elevations);
});

Though I really want to figure out a better way to do this with Restangular.

Any ideas would be greatly appreciated! Thanks!

4
  • Does this help ng-newsletter.com/posts/restangular.html @ the button of the page. Commented Apr 29, 2015 at 14:07
  • Thanks for the link, though it looks like that covers basically the same this as the documentation with better examples :). Commented Apr 29, 2015 at 14:09
  • 1
    sorry buddy I tried... I stopped using restangular and started to use angulars native facilities because they seem to be better documented. Commented Apr 29, 2015 at 16:25
  • 1
    Agreed--I'm heading in that direction shortly myself. It just seems to work easier. Commented Apr 29, 2015 at 18:27

1 Answer 1

1

That's deadly simple, querying a ressource URL without an id, even with specific query params, and returning a single object is just not RESTful.

Error: Response for getList SHOULD be an array and not an object or something else

It's precisely telling you the truth : your query could return an array, because you haven't specified a unique identifier (everything else is not unique).

At this point, it would be a magnitude easier to take Restangular out of the question and simply use the $http service

Definately, since Restangular focuses on RESTful resource proxying, or you're gonna have to tweak it (interceptors, customGET, ...)

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

3 Comments

Cyril--I'm not returning just a single object, though, I am retrieveing a list, it's just wrapped in metadata (in this case total record count is a sibling to the list itself). I am not querying for a unique ID because I'm looking for this list of all objects -- well, in this case, a subset of all of them -- a page. My whole point is that there should be an easier way to pass multiple query parameters to any of the GET methods Restangular provides.
Slashp. And my whole point is that I don't think you can, because Restangular focuses on RESTful APIs. You followed the exact right path, and Restangular is so well designed that it guided you through the process: 1) you firstly tried to use get, being aware your API returns a single json object, and it dodn't let you add some parameters, because it's designed to return a single object by id and doesn't require any query. 2) you tried querying getList, it told you that your API should return a list, or the response need to be customized with an interceptor.
Not that simple, I should say, indeed. But there isn't any other solution if you do want to use Restangular. You're maybe right thinking you shouldn't since it doesn't seems to fit your purpose. See RESTful

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.