I am pulling posts from WordPress blog site. But when I console log state posts
and response
in .then()
I get Response
as empty object [object object]
and state
as undefined
.
Where am I going wrong?
I am also getting following error:
TypeError: Cannot read property 'map' of undefined
Code:
import React, {Component} from 'react';
import axios from 'axios';
import Post from './Post/Post';
class Blog extends Component {
state = {
posts: []
}
ponentDidMount(){
axios.get(".1/sites/ishhaanpatel.wordpress/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;
I am pulling posts from WordPress blog site. But when I console log state posts
and response
in .then()
I get Response
as empty object [object object]
and state
as undefined
.
Where am I going wrong?
I am also getting following error:
TypeError: Cannot read property 'map' of undefined
Code:
import React, {Component} from 'react';
import axios from 'axios';
import Post from './Post/Post';
class Blog extends Component {
state = {
posts: []
}
ponentDidMount(){
axios.get("https://public-api.wordpress./rest/v1.1/sites/ishhaanpatel.wordpress./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;
Share
Improve this question
edited Jan 13, 2019 at 14:41
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked Jan 28, 2018 at 0:29
Ishan PatelIshan Patel
6,12112 gold badges51 silver badges70 bronze badges
3
-
When you set state is
posts
an empty or undefined array? – Mike Tung Commented Jan 28, 2018 at 0:34 -
just
console.log
the response... axios response return the data in the object data key. – Shibi Commented Jan 28, 2018 at 0:38 -
when I only return
response
,console.log
still giving meempty object
but this time I got state value asempty object
too. – Ishan Patel Commented Jan 28, 2018 at 0:44
2 Answers
Reset to default 2You are having problem with asyncronous
.
setState
is async
. So, you won't immediately get the value in this.state.posts
.
To solve this problem you can use callbacks as follows:
this.setState({ posts: response.posts }, () => {
console.log(this.state.posts);
});
Also your posts is nested inside response.data
. So, your setState
should look something like:
this.setState({ posts: response.data.posts }, () => {
console.log(this.state.posts);
});
Your data is nested inside the response.data
object.
Update
this.setState({posts: response.posts});
to
this.setState({posts: response.data.posts});
Axios returns a HTTP response object which contains additional information about the response.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745267338a4619537.html
评论列表(0条)