0

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

2 Answers 2

1

Your JSON is an array so you need to use an index or a loop to access one of your item like this:

ngOnInit(): void {
    this.testService.getTestWithObservable()
        .subscribe(
            res => res.forEach(user => this.user_data = [...(this.user_data || []), user['users']['data']]);
        );
}

Or:

ngOnInit(): void {
    this.testService.getTestWithObservable()
        .subscribe(
            res => this.user_data = res[0]['users']['data']);
        );
}

The first code will store in the user_data array all the data prop of each users

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I'm getting this: ERROR TypeError: Cannot read property 'concat' of undefined
Very interesting solution. Got another question, if you don't mind. After ngOnInit I got an array containing data to fill my table of users. I don't understand how to fill this array with the mapped result from the observable since I can't apply any logic outside of a function and if I write a function then call it on the same TS file I get "Function implementation is missing or not immediately following the declaration". Do you know why I get this?
@prevox > Can you ask a new question for your problem? Thank you
0

The result is array so you have to use index to access to the array data ,

    this.testService.getTestWithObservable()
     .map( result => result.map( i => i.users.data) )  // array of data array
     .subscribe(
        res => {

            this.user_data = [];
            res.forEach(i => this.user_data(...i));
            console.log(this.user_data); // all users data as array 
        }
    );
}

loop throw the data by ngFor

<div *ngFor="let user of user_data">
  {{user.name}} , age : {{user.age}}
</div>

6 Comments

How can I store "data" from both user 1 and 2 to use it later in a ngFor? Or should I iterate through the result directly?
the result is array so you can use it with ngFor
I can write an example if you want ??
That would be great, I'm not sure how to iterate through the array with ngFor yet
@prevox I have update the answer first I map the data to user data only so the resull is array of data array at the end I cobine all the data a single item array
|

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.