Reactjs React Axios get方法未显示任何数据,即使console.log输出显示数据
我正在控制台中获取数据,但无法在浏览器上显示。请让我知道我错在哪里。控制台数据也发送空白数组。 我的代码如下 ------------------------------------------------------Reactjs React Axios get方法未显示任何数据,即使console.log输出显示数据,reactjs,axios,Reactjs,Axios,我正在控制台中获取数据,但无法在浏览器上显示。请让我知道我错在哪里。控制台数据也发送空白数组。 我的代码如下 ------------------------------------------------------ import React,{Component}来自'React'; 从“antd”导入{Row,Col}; 导入“antd/dist/antd.css”; 导入“./orderform.css”; 从“axios”导入axios; 从“antd”导入{Card}; 从“antd
import React,{Component}来自'React';
从“antd”导入{Row,Col};
导入“antd/dist/antd.css”;
导入“./orderform.css”;
从“axios”导入axios;
从“antd”导入{Card};
从“antd”导入{Button};
类Orderform1扩展了组件{
建造师(道具){
超级(道具);
this.state={
项目组:[]
};
}
componentDidMount(){
axios.get()http://localhost:5000/api/itemgroup/get-项目组')
。然后(res=>{
this.setState({itemgroups:res.data}
);}
)
.catch(函数(错误){
console.log(错误);
})
}
render(){
//console.log(this.state.itemgroups);
const{itemgroups}=this.state;
console.log(项目组);
返回(
{
itemgroups.length>0&&
itemgroups.map(itemgroup=>
- {itemgroup.item\u group\u name}
)}
);}
}
导出默认Orderform1;
res.data包含一个对象,该对象包含您试图映射的数组。res.data包含一个对象,该对象包含您试图映射的数组。从屏幕截图上看,项目组
可能更深一层。设置状态时,请尝试this.setState({itemgroups:res.data.itemgroup})
从屏幕截图上看,itemgroups
可能更深一层。设置状态时,请尝试this.setState({itemgroups:res.data.itemgroup})
从屏幕截图上看,itemgroups
可能更深一层。当您设置状态时,请尝试this.setState({itemgroups:res.data.itemgroups})
从您的屏幕截图来看,itemgroups
可能更深一层。设置状态时,请尝试this.setState({itemgroups:res.data.itemgroups})
谢谢。问题已按照您的建议解决。谢谢Nick。问题已经按照你的建议解决了。
import React, { Component } from 'react';
import { Row, Col } from 'antd';
import "antd/dist/antd.css";
import './orderform.css';
import axios from 'axios';
import { Card } from 'antd';
import { Button } from 'antd';
class Orderform1 extends Component {
constructor(props) {
super(props);
this.state = {
itemgroups:[]
};
}
componentDidMount() {
axios.get('http://localhost:5000/api/itemgroup/get-itemgroup')
.then(res => {
this.setState({ itemgroups: res.data}
);}
)
.catch(function (error) {
console.log(error);
})
}
render() {
// console.log(this.state.itemgroups);
const {itemgroups} = this.state;
console.log(itemgroups);
return (
<div>
<ul>
{
itemgroups.length > 0 &&
itemgroups.map(itemgroup =>
<li key={itemgroup.item_group_id}>{itemgroup.item_group_name}</li>
)}
</ul>
</div>
);}
}
export default Orderform1;