最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting empty object in axios GET request - Stack Overflow

programmeradmin1浏览0评论

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 me empty object but this time I got state value as empty object too. – Ishan Patel Commented Jan 28, 2018 at 0:44
Add a ment  | 

2 Answers 2

Reset to default 2

You 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.

发布评论

评论列表(0)

  1. 暂无评论