0

When I perform a REST call (get) with axios from my react app, it says that the response is undefined. But when I try to do the same call in Postman, it clearly gives the right response. React cannot get the right response. How do I resolve this problem?

Response gives 'undefined' and response.data also gives 'undefined'.

class Blogpost extends React. Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isLoaded: false,
      items: []
    };
  }

componentDidMount() {
const response = axios.get({
      URL: 'http://178.62.198.162/api/posts',
      headers: {'token' : 'pj11daaQRz7zUIH56B9Z'}
    })
    const items = response.data;
    this.setState({
      isLoaded: false,
      items: items,
    });
    console.log(this.items);
  }

render() {
    const {error, isLoaded, items } = this.state;
    if (error) {
      return <div>Error: {error.message}</div>;
    } else if (!isLoaded) {
      return <div>Loading...</div>;
    } else {
      return (
        <ul>
          {this.items.map(item => (
            <li key={item.name}>
            {item.name}
            </li>
          ))}
        </ul>
      );
    }
  }
}

The expected result would be an array of collections. But actually it is given back 'undefined'.

How can I make it work so response gives the right array?

EDIT There seems to be a problem with the GET request. The promise gets rejected with fail code 404.

componentDidMount() {
const response = axios.get({
  URL: 'http://178.62.198.162/api/posts',
  headers: {'token' : 'pj11daaQRz7zUIH56B9Z'}
})
response.catch((error) => {
  console.log(error)
});

}

Is this a problem on the server side, or on the client side and how do I resolve it?

2
  • Hi Nynke! sarabs3 and I both provided answers for you to solve this problem. Honestly, both should work. Let us know if you have any questions. Commented May 9, 2019 at 9:30
  • Hi Christopher, There seems to be a problem with the promise. It gets rejected with fail code 404. I do not know if this is a server side problem or client side. Also because the call seems to work just fine in postman... Commented May 9, 2019 at 10:05

2 Answers 2

1

You are using axios wrong way. Axios always returns a Promise object.

Change your code to following and it will work.

componentDidMount() {
const response = axios.get({
      URL: 'http://178.62.198.162/api/posts',
      headers: {'token' : 'pj11daaQRz7zUIH56B9Z'}
    })
    response.then((res) => {
      const items = res.data;
      this.setState({
        isLoaded: false,
        items: items,
      });
      console.log(this.items);
    }
})

  }

You can read more about Promises here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Nynke, As your API is giving 404 error, you have already used catch() method of promise. that will handle the UI. and 404 error is basically either you are not calling the API endpoint currently or your server is not running. e.g. http://178.62.198.162/api/posts this link is not working. Not an issue with frontend
1

you should use async/await syntax to make sure response variable will be filled with right http get response before initializing

async componentDidMount() { 
  const response = await axios.get({
    URL: 'http://178.62.198.162/api/posts',
    headers: {'token' : 'pj11daaQRz7zUIH56B9Z'}
  })
  const items = response.data;
  this.setState({
  isLoaded: false,
  items: items,
});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.