-1

I am making an $http GET request to my REST api to get a list of projects. What I want to do is filter the response using the param option to only get projects assigned to a particular person.

var config = {
  withCredentials: true,
  params: {
    AssigneeId: 12423
  }
};
$http.get(baseUrl + 'projects', config).then(function successCallback(response) {
  //do things on success
}, function errorCallback(response) {
  //do things on error
});

The AssigneeId field is a property of the project object. The issue I am having is that the response for the $http.get is an array of JSON objects rendering the param useless. My response looks something like this:

[{
  ProjectName: 'project 1',
  AssigneeId: 12311,
  size: 5
}, {
  ProjectName: 'project 2',
  AssigneeId: 15232,
  size: 4
}, {
  ProjectName: 'project 3',
  AssigneeId: 43123,
  size: 2
}, {
  ProjectName: 'project 4',
  AssigneeId: 12423,
  size: 6
}]

What I wanted to do with the param was to only get 'project 4' as a response to my GET request. I think it is not working due to to the response being an array? Or am I doing something wrong?

2
  • If you want to filter it with params, the server has to support that... Commented Aug 24, 2016 at 15:57
  • you are asking for the api server to give you a filtered list, for this to hanpend you must have an API route that takes parameters something like /getProjectsById/{id} and have it filter the results for you. Also take a look here: en.wikipedia.org/wiki/Percent-encoding and here: stackoverflow.com/questions/21753072/… Commented Aug 24, 2016 at 16:06

2 Answers 2

0

As @Kevin B has mentioned it has to be supported by your server. You need to talk to your service guys to fix this or pass the right params. Till your server guy fixes this. You can have this new code

var assigneeId = 12423;
var config = {
  withCredentials: true
};
$http.get(baseUrl + 'projects', config )
 .then( successCallback(response){
   var assignedProject = response.filter(function(o){
     return o.AssigneeId === assigneeId; 
   }).map(function(o){
     return o.ProjectName;
   })[0];
 }, errorCallback(response){
    //do things on error
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Definitely will let the service guys know.
@randominstanceOfLivingThing You just copied my answer.
@SoftwareEngineer171, When I was adding my answer I only noticed the filter function. I did not see the map function.
0

You can filter it when you get response.

response = response.filter(function(a) {
  return a.AssigneeId === config.params.AssigneeId;
}).map(function(a) {
  return a.ProjectName;
})[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.