Reactjs在打印提取的数据时遇到问题

时间:2018-06-24 20:21:42

标签: javascript reactjs

超级菜鸟问题。但是,我已经在这个问题上停留了足够长的时间了。我一直在搜索,发现了类似的问题,但无法解决。我希望有人能指出我做错了正确的方向。

我正在从API提取数据,并且能够注销该阵列,但是,当我尝试将其打印出来时,我没有运气。一开始,我将其通过减速器,但后来意识到这并不理想。现在,我试图使其正常工作,然后将代码重构为功能组件。

fetchOrders = async () => {
        await axios.get('http://localhost:4200/ordenes').then( res => {
            this.setState({
                prescription: res.data
            })
        })
    }

    render() {
        console.log(this.state.prescription)
        return (
            <div >
                <Paper style={styles.orderCard}id='orderCardContainer' >
                    <div id="profileCardContainer" style={styles.profileCard}>
                        <aside className='profileCardContainer'>
                            <ProfileCard />
                        </aside>
                    </div>
                        {this.renderOrder()}
                </Paper>
            </div>
        )
    }
}

我从API收到的数组如下所示:

Array(18)
0: {order: Array(3), _id: "5b2af38fb315eb5630a0bc78", physicianName: "Alejandro", patientName: "Alex", createdAt: "2018-06-21T00:38:39.376Z", …}

更新:

class OrderCard extends Component {
    constructor(props) {
        super(props)
        this.state = { prescription: []}
    }

    fetchOrders = async () => {
        const res = await axios.get('http://localhost:4200/ordenes')
        this.setState({
            prescription: res.data
        })
    }

    componentDidMount() {
        this.fetchOrders()
    }

    renderOrder() {
        if (this.state.prescription.length === 0 ) {
            return ( 
                <h1>Loading...</h1>
            )
        } else {
            return (
                this.state.prescription.map( (x, i) => {
                    <li>
                        {console.log(x.patientName)}
                        <h1>{ x.patientName }</h1>
                    </li>
            })
        )}
    }

    render() {
        console.log(this.state.prescription)
        return (
            <div >
                <Paper style={styles.orderCard}id='orderCardContainer' >
                    <div id="profileCardContainer" style={styles.profileCard}>
                        <aside className='profileCardContainer'>
                            <ProfileCard />
                        </aside>
                    </div>
                    <div id='orders' >
                        { this.renderOrder() }
                    </div>
                </Paper>
            </div>
        )
    }
}

function mapStateToProps(state) {
    return {
        auth: state.auth.authenticated,
    }
}

export default connect(mapStateToProps, actions)(OrderCard)

BACKEND SCHEMA

const orderObj = new Schema({
    name: String,
    price: Number,
 }, { _id : false })

const orderSchema = new Schema (
    {
        physicianName: String,
        patientName: String,
        order: {type: [orderObj]}
    }, 
    { 
        timestamps: true
    }
)

POSTMAN API结果

[
    {
        "order": [
            {
                "name": "Asteraceae",
                "price": 41.24
            },
            {
                "name": "Liliaceae",
                "price": 39.24
            },
            {
                "name": "Fabaceae",
                "price": 34.91
            }
        ],
        "_id": "5b2af38fb315eb5630a0bc78",
        "physicianName": "Alejandro",
        "patientName": "Alex",
        "createdAt": "2018-06-21T00:38:39.376Z",
        "updatedAt": "2018-06-21T00:38:39.376Z",
        "__v": 0
    }
]

预先感谢您的提示。

1 个答案:

答案 0 :(得分:1)

括号错误,您没有返回react元素,请尝试固定一个。

renderOrder() {
    if (this.state.prescription.length === 0 ) {
        return ( 
            <h1>Loading...</h1>
        )
    } else {
        return (
            this.state.prescription.map((x, i) => (
                <li>
                    {console.log(x.patientName)}
                    <h1>{ x.patientName }</h1>
                </li>
        ))
    )}
}

并解决此问题

fetchOrders = async () => {
  const res = await axios.get('http://localhost:4200/ordenes');
  this.setState({
    prescription: res.data
  });
}