【发布时间】:2019-11-15 23:04:35
【问题描述】:
我在控制台中获取数据,但无法在浏览器上显示。请让我知道我错在哪里。控制台数据也发送空白数组。 我的代码如下 -------------------------------------------------- ----Image
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;
【问题讨论】:
-
从您的屏幕截图看来,
itemgroups可能更深一层。当你设置状态时,试试this.setState({ itemgroups: res.data.itemgroups})