restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Calling an object with .one() and .get() returns an object wrapped in an array. It should return the object.

Open davemerwin opened this issue 11 years ago • 11 comments

For example: (uses ui-router)

Restangular.one('items', $stateParams.itemId).get().then(function(item){
    console.log(item.id);
    $scope.item = item;
});

The above returns an array with the object. But the same call to the same resource with Postman or the Django Rest Framework browser return the object.

This is designed to be a detail view, and I would assume that it would return the object. Any suggestions on what is going on? What have I missed?

davemerwin avatar Jul 30 '14 01:07 davemerwin

Looks good to me, it should be an object indeed. I would love to investigate further, but without any sample of JSON response, that will be hard.

pauldijou avatar Aug 04 '14 06:08 pauldijou

So, with Postman I get this:

{"id": 1, "url": "http://127.0.0.1:8000/api/items/1", "title": "Awesomeness", "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "category": ["http://127.0.0.1:8000/api/categories/1"], "tags": ["http://127.0.0.1:8000/api/tags/2", "http://127.0.0.1:8000/api/tags/1"]}

With Restangular I get this:

[{"id": 1, "url": "http://127.0.0.1:8000/api/items/1", "title": "Awesomeness", "summary": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "category": ["http://127.0.0.1:8000/api/categories/1"], "tags": ["http://127.0.0.1:8000/api/tags/2", "http://127.0.0.1:8000/api/tags/1"]}]

davemerwin avatar Aug 04 '14 14:08 davemerwin

Ok, I tried to reproduce but it works just fine.

Do you have any interceptor? Or $http interceptor? Or funny configuration?

pauldijou avatar Aug 06 '14 06:08 pauldijou

I do not have any interceptors. I also just reinstalled everything to make sure that there wasn't anything that could also be causing an issue. I also don't have any funny configurations.

Furthermore, a curl to the api returns an object. Not an array.

Do you have suggestions on how to troubleshoot this?

davemerwin avatar Aug 06 '14 15:08 davemerwin

Also, I rewrote the entire thing using $http only, and it returns the object properly. Something about the wrapper of Restangular is causing it to become an array.

davemerwin avatar Aug 06 '14 15:08 davemerwin

Here is a try to reproduce the problem: http://jsbin.com/sotagesu/2/edit?js,output . But as you can see, it returns a real object, not an array. Could you try to play with it, eventually remove the $httpBackend and call a real API. If you can get the wrapped array, it will be all good.

As writing the sample, something comes to my mind, you are calling .get() and not .getList(), right?

pauldijou avatar Aug 06 '14 22:08 pauldijou

Correct. I'm using get(). I'll take a look at what you posted.

On Wed, Aug 6, 2014 at 3:22 PM, Paul Dijou [email protected] wrote:

Here is a try to reproduce the problem. But as you can see, it returns a real object, not an array. Could you try to play with it, eventually remove the $httpBackend and call a real API. If you can get the wrapped array, it will be all good.

As writing the sample, something comes to my mind, you are calling .get() and not .getList(), right?

— Reply to this email directly or view it on GitHub https://github.com/mgonto/restangular/issues/806#issuecomment-51405869.

Dave Merwin

http://facebook.com/davemerwin http://twitter.com/davemerwin http://www.davemerwin.com My Profile: http://www.linkedin.com/in/merwin 541.335.1832

davemerwin avatar Aug 07 '14 00:08 davemerwin

I have this same problem :[ I thought I was totally crazy, I'm glad this issue is here.

  $scope.setCurrentUser = function(userId) {
    User.find(userId).then(function(user) {
      $scope.currentUser = user;
    });
  };

and

'use strict';


app.factory('User', function(Restangular) {

  var _user = Restangular.all('users');

  return {
    all: function() {
      return _user.getList();
    },
    find: function(id) {
      return Restangular.one('user', id).get();
    }
  };

});

Returns [Object, restangularEtag: "W/"c7-2425979322"", route: "user", getRestangularUrl: function, getRequestedUrl: function, addRestangularMethod: function…]

I have to do this to get it to work in the view:

$scope.currentUser = user[0];

or if I just use {{currentUser}} in the view I get [{"id":1,"company_id":1,"username":"admin","email":"[email protected]"}]

9mm avatar Aug 22 '14 21:08 9mm

Has this issue been resolved?

daviesgeek avatar Jun 17 '16 04:06 daviesgeek

Not sure how helpful this will be, but I was able to reproduce this error when the ID I gave Restangular was undefined and my service/database only had 1 record to return.

valevalorin avatar Nov 03 '16 01:11 valevalorin

I faced with the same issue. The problem is that the response json file wasn't designed the way restangular service was expecting. Restangular expects the data to be in object tag. This one will work.

json {
    errors null
    object { id :100  
  }
}

This one won't work.

json {
id:100
}

etibarhasanov avatar Jul 06 '17 09:07 etibarhasanov