reactjs - TypeError:this.state.employees.map 不是函数
问题描述
在这里,我使用状态中的员工作为对象。
map is not a function
尝试将其呈现为表格时出现此错误。后端 api 正在返回employees
.
class Employee extends React.Component{
constructor(props){
super(props);
this.state={
employees:[]
};
}
componentDidMount(){
fetch("http://localhost:8080/employee/getEmployees").then(res=>res.json).then(
result=>{
console.log(result.id)
this.setState({employees:result});
}
)
}
render(){
return (<div>
<h2>welcome to employee.</h2>
<table>
<thead>
<tr>
<th>Employee id</th>
<th>Employee name</th>
<th>Employee age</th>
<th>Employee companyid</th>
<th>Employee stream</th>
</tr>
</thead>
<tbody>
{this.state.employees.map(emp=>(
<tr key={emp.id}>
<td>{emp.id}</td>
<td>{emp.name}</td>
<td>{emp.age}</td>
<td>{emp.companyId}</td>
<td>{emp.stream}</td>
</tr>
))}
</tbody>
</table>
</div>)
}
}
ReactDOM.render(<Employee />, document.getElementById("root"))
解决方案
您正在将一个函数传递给第二个then
未解析为 JSON 的结果。
只需更改res=>res.json
为res=>res.json()
.
推荐阅读
- mysql - 使用 Python 设置将插入 MySQL 的温度和湿度的最大值和最小值
- python - pip raises ProtocolError('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
- c# - 不可转换的委托返回类型在哪里?
- sms - SMS Manager - 自动 OTP 检测
- python - 如何使用 matplotlib.pyplot,Python3 为每个表添加名称/文本
- node.js - 单击按钮将数据从客户端发送到服务器
- amazon-web-services - 在 Codebuild 中构建和发布 dotnet 核心应用程序
- vuetify.js - 将 Vuetify 与 Jhipster 一起使用
- c# - 如何在 Windows 11 中使用 win32 或 uwp 在任务栏中制作动画图标
- sql - 带有 IN 运算符的 Suitescript SuiteQL 错误