【发布时间】:2018-07-07 00:15:47
【问题描述】:
我正在从 WordPress 博客网站中提取帖子。但是,当我在.then() 中控制台记录状态posts 和response 时,我得到Response 作为empty object [object object] 和state 作为undefined。
我哪里错了?
我也收到以下错误:
TypeError: 无法读取未定义的属性“地图”
代码:
import React, {Component} from 'react';
import axios from 'axios';
import Post from './Post/Post';
class Blog extends Component {
state = {
posts: []
}
componentDidMount(){
axios.get("https://public-api.wordpress.com/rest/v1.1/sites/ishhaanpatel.wordpress.com/posts")
.then( response => {
this.setState({posts: response.posts});
console.log("Here are the posts: "+ this.state.posts);
console.log("Here is the response: "+ response);
});
}
render(){
const posts = this.state.posts.map( post => {
return <Post title={post.title} key={post.ID} author={post.author.name} />;
});
return (
<div>
{posts}
</div>
);
}
}
export default Blog;
【问题讨论】:
-
当你设置state是
posts一个空的或者未定义的数组? -
只是
console.log响应... axios响应返回对象数据键中的数据。 -
当我只返回
response,console.log仍然给我empty object但这次我的状态值也为empty object。
标签: javascript reactjs get typeerror axios