Hi all,

I would like to expose a problem that I'm facing sending complex objects as 
parameters to ngResource. I'm trying to do the following query with 
ngResource:

var Customer = $resource('http://localhost:3000/api/users');
var params = 
{
conditions: {
age: { "$gt": 30 }
},

options: {
limit: 2
}
};

Customer.query(params, 
function(data) {
$scope.users = data;
}
);
And in the browser console I get the following error:

GET 
http://localhost:3000/api/users?conditions=%7B%22age%22:%7B%7D%7D&options=%7B%22limit%22:2%7D
 
400 (Bad Request) 

As you can see, the parameters that I'm sending are being escaped, and this 
is causing a bad request.

If I try to do the following request through $http I don't get any problem:

$http.get('http://localhost:3000/api/users?conditions={"age":{"$gt":30}}&options={"limit":2}').success(function(data)
 
{
            $scope.users = data;
  });

So, I have to questions:

1) Is there anyway to avoid the problem that I'm facing when sending 
complex parameters to ngResource.

2) Do you think that is a good practice sending parameters as I'm doing in 
my previous examples? Or do you think that would be better in this way: 
http://localhost:3000/api/users?age-gt=30&limit=2 ? Undoubtedly, this last 
way is much cleaner, but I think that in my previous examples I have a lot 
of flexibility, because this is the way that MongoDB uses to query its 
collections, and as I have a MongoDB in my back-end then I can send 
directly the parameters to the MongoDB query. 

I didn't like brackets in the URL, but I have seen that parse.com uses a 
similar syntax to query its data:

curl -X GET \
  -H "X-Parse-Application-Id: ${APPLICATION_ID}" \
  -H "X-Parse-REST-API-Key: ${REST_API_KEY}" \
  -G \
  --data-urlencode 'where={"score":{"$gte":1000,"$lte":3000}}' \
  https://api.parse.com/1/classes/GameScore
  
I would appreciate a lot your comments regarding these two questions.

Thank you very much!!

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to