【发布时间】:2019-08-19 17:57:22
【问题描述】:
我想从 Json Server 获取数据,我需要在哪里编辑?
这是我得到的错误
"TypeError: 无法读取未定义的属性 'map'"
getPosts() {
axios.get("http://localhost:8001/employees")
.then(response => {
this.setState({
posts : response.data.posts,
isLoading: false
});
})
.catch(error => this.setState({error, isLoading : false}));
}
componentDidMount () {
this.getPosts();
}
render() {
const {isLoading , posts} = this.state;
return (
<React.Fragment>
<h2>Data JSON</h2>
<div>
{!isLoading ? (
posts.map(post => {
const {id, first_name, last_name, email} = post;
return (
<div>
<h2>{id}</h2>
<h2>{first_name}</h2>
<p>{last_name}</p>
<p>{email}</p>
<hr />
</div>
);
})
) : (
<p>Loading...</p>
)}
</div>
</React.Fragment>
);
}
}
导出默认登录;
我希望来自 json 服务器的数据会出现,但它不会出现
【问题讨论】:
-
发布完整的组件以及您收到的服务器的响应。您面临的错误意味着您正在尝试从不是 array 的数据中访问属性映射
标签: reactjs