地图 API JSON 获取
我在将 Laravel API 的数据放入 React 时遇到问题,在获取并引入数据时,JSON 数据毫无问题地出现在控制台中,但是当我想将它们放入表格中列出它们时,会出现错误并说我的 .map
不是一个函数,我希望你能帮助我。
import React from 'react';
class Crudcategories extends React.Component{
state = {
categories:[]
}
componentDidMount(){
fetch("http://localhost:8000/api/categories")
.then(response => response.json())
.then(categoriesJson => this.setState({categories:categoriesJson}))
}
render(){
const{categories} =this.state
return(
<div>
<h1>Categories</h1>
<br/>
<table className="table table-striped table-border table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
{categories.map((category,i)=>
<tr key={i}>
<th scope="row">{i+1}</th>
<td>{category.name}</td>
</tr>
)}
</tbody>
</table>
</div>
)
}
}
export default Crudcategories
我尝试过更改我的获取、我的地图,但无法解决
I have problems when putting the data of a Laravel API in React, when doing the fetch and bringing the data, the JSON data appears in the console without problems, but when I want to put them in a table to list them, an error appears and says that my .map
is not a function, I hope you can help me.
import React from 'react';
class Crudcategories extends React.Component{
state = {
categories:[]
}
componentDidMount(){
fetch("http://localhost:8000/api/categories")
.then(response => response.json())
.then(categoriesJson => this.setState({categories:categoriesJson}))
}
render(){
const{categories} =this.state
return(
<div>
<h1>Categories</h1>
<br/>
<table className="table table-striped table-border table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
</tr>
</thead>
<tbody>
{categories.map((category,i)=>
<tr key={i}>
<th scope="row">{i+1}</th>
<td>{category.name}</td>
</tr>
)}
</tbody>
</table>
</div>
)
}
}
export default Crudcategories
I have tried to change my fetch, my map, but I can't solve it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在 JSON 对象上使用 .map() ,但这是行不通的,因为对象没有 .map() 函数。
尝试将 JSON 数据转换为对象数组,然后尝试 .map()
示例:
然后使用 categoriasArr.map()
It seems like you're using .map() on a JSON object, which won't work, as objects do not have a .map() function.
Try converting your JSON data to an array of objects then try .map()
Example:
Then use categoriasArr.map()