【问题标题】:React Axios get method is not displaying any data even though console.log output shows data即使 console.log 输出显示数据,React Axios get 方法也不显示任何数据
【发布时间】: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})

标签: reactjs axios


【解决方案1】:

从您的屏幕截图看来,itemgroups 可能更深一层。当你设置状态时,试试this.setState({ itemgroups: res.data.itemgroup })

【讨论】:

  • 谢谢尼克。根据您的建议,问题已解决。
【解决方案2】:

res.data 包含一个对象,该对象包含您要映射的数组

【讨论】:

    猜你喜欢
    • 2020-01-20
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多