Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davemerwin opened this issue Jul 30, 2014 · 11 comments

Comments

@davemerwin
Copy link

@davemerwin davemerwin commented Jul 30, 2014

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?

@pauldijou
Copy link
Contributor

@pauldijou pauldijou commented Aug 4, 2014

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 pauldijou closed this Aug 4, 2014
@pauldijou pauldijou reopened this Aug 4, 2014
@davemerwin
Copy link
Author

@davemerwin davemerwin commented Aug 4, 2014

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"]}]

@pauldijou
Copy link
Contributor

@pauldijou pauldijou commented Aug 6, 2014

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

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

@pauldijou pauldijou self-assigned this Aug 6, 2014
@davemerwin
Copy link
Author

@davemerwin davemerwin commented Aug 6, 2014

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
Copy link
Author

@davemerwin davemerwin commented Aug 6, 2014

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.

@pauldijou
Copy link
Contributor

@pauldijou pauldijou commented Aug 6, 2014

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?

@davemerwin
Copy link
Author

@davemerwin davemerwin commented Aug 7, 2014

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 notifications@github.com 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
#806 (comment).

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

@9mm
Copy link

@9mm 9mm commented Aug 22, 2014

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":"admin@example.com"}]

@daviesgeek
Copy link
Collaborator

@daviesgeek daviesgeek commented Jun 17, 2016

Has this issue been resolved?

@valevalorin
Copy link

@valevalorin valevalorin commented Nov 3, 2016

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.

@etibarhasanov
Copy link

@etibarhasanov etibarhasanov commented Jul 6, 2017

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.