I am trying to obtain an array of information from a GET
request in Angular 2. To do so, I created a service as follows:
obtainInformation() {
// Obtain authorization token for the request
const authToken = localStorage.getItem('auth_token');
// Configure headers
const headers = new Headers();
headers.append('x-access-token', authToken);
// Execute request
return this.http.get(this.clubInfoUrl, { headers: headers })
.map(res => res.json())
.catch(this.handleError);
}
Then, I call this service from an info.component.ts
as follows (for simplification I add only the exact part of the code):
this.service.obtainInformation()
.subscribe(res=>{
console.log(res);
});
With this, I get an object with two strings and array of information (which is the array I want to process), but I am not sure about how I can pick the array.
I tried with JSON.stringify()
, but then I cannot do (for example):
res.club_info.id
Notice that club_info is the array, and id is one of the elements of the array.
Any recommendation about it?
Thank you in advance!
Edit: output of console.log(res)
Make sure you include the index of the array when accessing.
You said:
Notice that club_info is the array, and id is one of the elements of the array.
club_info
is an Array. The first element of the array is a Javascript object, so you'll need to provide the array index to access the object. In addition, the attribute id
does not exist, but perhaps you meant id_number
or _id
.
Thus, res.club_info[0].id_number
Clearly you got an array with 1 element:
you should call:
let first_element = res.club_info[0];
let first_element_id = first_element._id
Good Luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.