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

78
Vistas
Displaying data from API undefined

I'm trying to display student data and the courses they are enrolled in from a a simple API I made.

Here's the result from the API https://localhost:44309/api/Students

[
  {
    "id": 1,
    "lastName": "Rizal",
    "firstName": "Jose",
    "courses": [
      "C#",
      "Javascript",
      "CSS"
    ]
  },
  {
    "id": 2,
    "lastName": "Bonifacio",
    "firstName": "Andres",
    "courses": [
      "HTML",
      "ASP.NET MVC"
    ]
  },
  {
    "id": 3,
    "lastName": "Sora",
    "firstName": "Tandang",
    "courses": [
      "CSS",
      ".NET"
    ]
  }
]

Here's my Javascript code

import React from "react"
import { useState, useEffect } from "react";


const StudentList = () => {
    const [students, setStudent] = useState([]);
    
    const getData = async () => {
      const response = await fetch("https://localhost:44309/api/students");
      const data = await response.json()
      setStudent(data)
    }
  
    useEffect(() => {
        getData()
    }, []);

    return (
        <div className="student-list">
            <h2>Students and their courses</h2>
            {students.map(student => (
                <div className="student-preview" key={student.id}>
                    <p>{ student.fname } { student.lname }</p>
                    <p>{ student.courses }</p>
                </div>
            ))}
        </div>
    )
}

export default StudentList

I'm getting an error saying Uncaught ReferenceError: students is not defined

I wonder what I'm doing wrong.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Something is wrong with your api the code works fine with static data

import React from "react";
import { useState, useEffect } from "react";

const StudentList = () => {
  const [students, setStudent] = useState([
    {
      id: 1,
      lastName: "Rizal",
      firstName: "Jose",
      courses: ["C#", "Javascript", "CSS"]
    },
    {
      id: 2,
      lastName: "Bonifacio",
      firstName: "Andres",
      courses: ["HTML", "ASP.NET MVC"]
    },
    {
      id: 3,
      lastName: "Sora",
      firstName: "Tandang",
      courses: ["CSS", ".NET"]
    }
  ]);

  return (
    <div className="student-list">
      <h2>Students and their courses</h2>
      {students.map((student) => (
        <div className="student-preview" key={student.id}>
          <p>
            {student.fname} {student.lname}
          </p>
          <p>{student.courses}</p>
        </div>
      ))}
    </div>
  );
};

export default StudentList;
about 4 years ago · Juan Pablo Isaza Denunciar
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