restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Polluted result from getList()

Open mbhenning opened this issue 12 years ago • 3 comments

Hi, I'm trying to send a GET to /api/users/:

var restAngular = Restangular.withConfig(function(config) {
    config.setBaseUrl('/api');
  });

  var _userService = restAngular.all('users');

  var userService = {
    getUsers: function () {
      return _userService.getList().$object;
    }
  };

  return userService;

I inject the service into a controller, then call the userService.getUsers(). The result should be [{ lastname: 'Doe', firstname:'John', _id:..}], and this data is retrieved.

However, the returned object is polluted with restangular-specific properties:

userList: 
[  { 
lastname: Doe
firstname: John
_id: 53517ecbfa04c8a7de000001
__v: 0
route: users
getRestangularUrl: null
getRequestedUrl: null
addRestangularMethod: null
clone: null
reqParams: null
withHttpConfig: null
plain: null
one: null
all: null
several: null
oneUrl: null
allUrl: null
fromServer: true
parentResource: null
getParentList: null
restangularCollection: false
get: null
getList: null
put: null
post: null
remove: null
head: null
trace: null
options: null
patch: null
save: null
customOperation: null
doPUT: null
customPUT: null
doPOST: null
customPOST: null
doGET: null
customGET: null
doDELETE: null
customDELETE: null
customGETLIST: null
doGETLIST: null
 }  ]

I've tested it with AngularJS v.1.2.16 + 1.3.0 and Restangular 1.3.1. I don't think it's related to the getList().$object, because I've also tried the regular way: getList().then(function(res){ $scope.xyz = res })..

I would appreciate any help on this.

mbhenning avatar May 11 '14 21:05 mbhenning

Even better: one of my endpoints returns a property called options. Since Restangular has an options() method that's used to do an OPTIONS HTTP request, it happily overrides my property, and, of course, calling stripRestangular() just leaves me with no options property at all…

dmkc avatar May 15 '14 21:05 dmkc

Thanks for the report!

@mbhenning the solution for your problem is calling stripRestangular method to get the clean object if you don't want the Restangular stuff

@dmkc all of Restangular methods are configurable. You can just set the following:

RestangularProvider.setRestangularFields({
    "options": "restangularOptions"
});

Now, restangular method will be another one and it won't override your code.

Hope it helps :).

Please do let me know if it does.

Thanks!

mgonto avatar May 30 '14 01:05 mgonto

@mgonto Thanks, that is a decent workaround!

dmkc avatar Jun 04 '14 19:06 dmkc