.map不是一个函数.....不渲染表

为什么不想显示我的数据表?

我不明白为什么它不想在表中显示我的数据。 我console.log一切,并在控制台中显示结果。 当我转到“网络-预览并响应”时,它将显示包含所有信息的完整json,但我的表为空......

import React,{Component} from 'react';

class ProfilStudent extends Component {
    constructor(){
        super();
        this.state={
                isLoading:false,isError:false,students: []
        };
    }
    componentDidmount() {
    fetch('http://localhost:8095/dnevnik/student/profil',{
            method: 'GET',headers: {
                'Authorization': 'Basic ' + window.btoa(this.props.username + ":" + this.props.password),"Content-type": "application/json; charset=UTF-8",'accept': 'application/json',}
        })
        .then(response => {
            return response.json();
        })
        .then(students => {
        this.setState({ students });
        });
    }
    renderStudents() {
        return this.state.students.map(student => {
          return (
            <tr key={student.id}>
              <td>{student.id}</td>
              <td>{student.firstName}</td>
              <td>{student.lastName}</td>
            </tr>
          );
        })
      }

    render() {
      
        return (
            <div>
               <table>
                   <thead>
                       <tr>
                           <th>ID</th>
                           <th>firstName</th>
                           <th>lastName</th>
                           <th>username</th>
                       </tr>
                   </thead>
                   <tbody>
                       {this.renderStudents()}
                   </tbody>
              </table>
              </div>
            );  
        }     
}
export default ProfilStudent;

我已经尝试了一切。搜索了Internet,stackoverflow上的所有信息,但仍然存在此问题 我在其他班上有相同的代码,并且运行良好。 请问是什么原因,请帮忙!

zucker19782002 回答:.map不是一个函数.....不渲染表

检查API的响应

.then(students => {
        this.setState({ students });
        });

在学生中,您可能会得到对象,然后尝试用对象替换学生数组。

您可以使用map进行数组迭代。

,

谢谢。 我非常疲倦,因为我没有注意到我有对象而不是数组,所以我对响应进行了数组设置,现在映射工作了。 谢谢大家

本文链接:https://www.f2er.com/3163150.html

大家都在问