Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

121
Views
Can't access JSON object's variables how I have been doing it?

I am returning a list of 2 objects from my API and have used a fetch in the profile component successfully to set the state of userStats to be this JSON object:

[{"userStatsId":1,"victories":0,"draws":0,"defeats":0},{"userStatsId":2,"victories":0,"draws":0,"defeats":0}]

This is also what prints in console in the code below, so the state is definitely set correctly and passed into the render method.

Previously I was being returned JSON objects containing those UserStat objects and I was accessing the values using:

<td className="player-list-key-number">{user.userStats[0].victories}</td>

So I just assumed now that I am simply returning the list of the 2 UserStat objects logically I should just do:

<h3 id="stats">Stats</h3>
                        <CardGroup>
                        <Card className="stat-card">
                            <CardImg></CardImg>
                            <CardTitle>Total Victories</CardTitle>
                            <CardText className="victory-text">{userStats[0].victories}</CardText>
                        </Card>

Etc...

However, this causes an error:

Uncaught TypeError: Cannot read properties of undefined (reading 'victories')

In Profile component:

render() { const {user, profileImage, userStats, isLoading} = this.state;

        console.log(this.props.location);
        console.log("USER:");
        console.log(user);

        console.log("USERSTATS:");
        console.log(userStats);


        if (isLoading) {
            return <p>Loading...</p>;
        }

        return (

<h3 id="stats">Stats</h3>
                            <CardGroup>
                            <Card className="stat-card">
                                <CardImg></CardImg>
                                <CardTitle>Total Victories</CardTitle>
                                <CardText className="victory-text">{userStats[0].victories}</CardText>
                            </Card>
                            <Card className="stat-card">
                                <CardTitle>Total Defeats</CardTitle>
                                <CardText className="defeats-text"></CardText>
                            </Card>
)

How can I access these values now they are not in outer User object?

Thanks

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!