restangular
restangular copied to clipboard
Calling an object with .one() and .get() returns an object wrapped in an array. It should return the object.
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?
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.
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"]}]
Ok, I tried to reproduce but it works just fine.
Do you have any interceptor? Or $http interceptor? Or funny configuration?
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?
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.
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?
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
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]"}]
Has this issue been resolved?
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.
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
}