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

107
Vistas
React-Frontend showing list of mongodb collection entries

I have a backend-API server (FastAPI) where i can post/get/update/delete items in a MongoDB collection. I now want to show these items in an React-Frontend, which fetches the API like shown in this code:

import React, { Component } from 'react';

class App extends Component {
constructor(props) {
    super(props);
    this.state = {
      items: []
    }
  }
  componentDidMount() {
    const url = "http://localhost:8000/items";
    fetch(url)
    .then(response => response.json())
    .then(json => this.setState({ items: json }))
  }
  render() {
    const { items } = this.state;
    console.log(items)
    console.log(Object.keys(items))
    return (
      <div className="container">
        <div class="jumbotron">
          <h1 class="display-4">Items from our API call</h1>
        </div>
        {/* because we have a dict not an array */}
        {Object.keys(items).map((item) => (
          <div className="card" key={item._id}>
            <div className="card-header">
              ITEM #{item.name} {item.type.name}
            </div>
            <div className="card-body">
              PRICE: {item.price}
            </div>
          </div>
        ))}
      </div>
    );
  }
}
export default App;

The site doesn't show anything, if i look into Development-Tools it says "Uncaught TypeError: Cannot read properties of undefined (reading 'name')."

I cant figure how to fix this typeerror, the API has this format:

{
    "items": [
        {
            "_id": "6273be87b410b45e0f438df4",
            "name": "Fifa22",
            "price": 40,
            "type": "hardware"
        }
    ]
}
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