I'm trying to extract "data" from this JSON but it keeps throwing undefined as result.
Extract of code where I'm getting this error:
ngOnInit(): void {
this.testService.getTestWithObservable().subscribe(
res => {
let user = res["users"];
let user_data = user["data"];
this.user_data = user_data;
console.log(user_data);
console.log(res);
}
);
}
res outputs the whole JSON but user_data throws:
ERROR TypeError: Cannot read property 'data' of undefined at SafeSubscriber._next
JSON
[{
"id": 1,
"users": {
"user_id": 14,
"data": [{
"name": "James",
"age": 20
},
{
"name": "Damien",
"age": 25
}]
}
}
{
"id": 2,
"users": {
"user_id": 11,
"data": [{
"name": "James",
"age": 20
},
{
"name": "Damien",
"age": 25
}]
}
}]