Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am new to AngularJS. Trying to get specific element from a JSON array via $resource. The structure of JSON file staffs.json is like:

[{
    "id": 0,
    "facility_id": [0],
    "name": "Tim",
    "role_id": 0
},
{
    "id": 1,
    "facility_id": [0],
    "name": "Duncan",
    "role_id": 0
},
{
    "id": 2,
    "facility_id": [0],
    "name": "Tony",
    "role_id": 1
},
{
    "id": 3,
    "facility_id": [0],
    "name": "Parker",
    "role_id": 1
},
{
    "id": 4,
    "facility_id": [0],
    "name": "Manu",
    "role_id": 2
},
{
    "id": 5,
    "facility_id": [0],
    "name": "Ginobili",
    "role_id": 2
},
{
    "id": 6,
    "facility_id": [0],
    "name": "Tiago",
    "role_id": 3
},
{
    "id": 7,
    "facility_id": [0],
    "name": "Splitter",
    "role_id": 3
}]

I am trying to get a staff whose name is "Tiago". The code is:

var url = 'data/staffs.json';
var username = 'Tiago';
users = $resource(url);
users.get({name: username}, function(data){
    alert(data.name);
});

It seems the alert() function inside the get() never gets called. However if I changed the method from users.get() to users.query(), it can get the list of the staffs. I guess this is because the data inside the JSON file is an array, so the query() which is used to get array works, while the get() does not work because it is not for array operation. Am I correct?

I am just wondering if I have to use query() get the whole array and match the elements one by one until I find the one with the same name, or there are some simpler ways to get the element I want.

Thanks

share|improve this question
    
Don't you need in your url the parameter? data/:username/staffs.json – lukas Jul 22 '14 at 16:39
    
@lukas I think the url should be the path to the json file, which is data/staffs.json – shaosh Jul 22 '14 at 16:44
    
so why do you use {name: username} ? – lukas Jul 22 '14 at 16:45
    
@lukas {name: username} is used to get the name field whose value is username in the response object. – shaosh Jul 22 '14 at 16:49
    
@lukas if you don't specify the path parameter the $resource module will append the variable as a query parameter. In this case the api call will be: "..data/staffs.json?name=Tiago" – Heikki Jul 22 '14 at 20:14

2 Answers 2

up vote 2 down vote accepted

AngularJS resource has a separate query function to avoid JSONP vulnerability for arrays. You have two options:

  • get all and find the element in the array on the client side
  • add extra API endpoint for single user and fetch it by the name

I vote for option two, since you don't have to send everything over the wire and you use server (DB) to get the specific user. Server software is optimised for that.

share|improve this answer
    
The complete version of my code will work like option 2, but before that part is finished, seems I have to apply the option 1 to test the client functions. – shaosh Jul 22 '14 at 20:41

Your best bet would by fetching data with the query() method, then using indexOf()

var url = 'data/staffs.json';
var username = 'Tiago';
users = $resource(url);
users.get({name: username}, function(data){
    dataOfMyUser = data.map(function(cur) { return cur.name }).indexOf(username);
    alert(dataofMyUser);
});
share|improve this answer
    
cur.type? do you mean cur.name perhaps? – Heikki Jul 22 '14 at 20:07
    
Yes--sorry about that typo! – srph Jul 25 '14 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.