Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

124
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda