Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda