Roosh1r Roosh1r - 3 months ago 18
AngularJS Question

(Angular JS) How filter $http.get by passing params if the response is an array of JSON objects?

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( successCallback(response){
//do things on success
}, 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?

Any help would be appreciated.

Answer

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
 }
Comments