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?