【发布时间】:2017-06-12 18:12:22
【问题描述】:
我从服务器接收数据作为 json 数据,但无法在浏览器上显示它我收到错误
" 对象作为 React 子对象无效(找到:带键的对象 {_id,ID,姓名,年龄})。如果你打算渲染一个集合 孩子,使用数组代替或使用包装对象 来自 React 附加组件的 createFragment(object)。检查渲染方法 在学生”
import React from 'react';
import axios from 'axios';
class Student extends React.Component{
constructor(props){
super(props);
this.state={
post:[]
}
};
componentDidMount(){
axios.get('http://localhost:8080/student')
.then(data => this.setState({post:data}));
}
render(){
return(
<div className="pre">
<h1>{this.state.post.data}</h1>
</div>
);
}
}
export default Student;
【问题讨论】:
-
axios 返回包含数据属性的响应对象。使用
data.data -
你能不能在 axios .then 函数中记录数据并将结果包含在帖子中