I am trying to call an API from React but it keeps returning as undefined. This is my code:
import React from 'react';
export default class UserList extends React.Component {
constructor(props) {
super(props);
this.state = { customer: [] };
}
componentDidMount() {
fetch('https://recommenderapi.herokuapp.com/customer_id=x', {
mode: 'no-cors',
method: "post",
headers: {
"Content-Type": "application/json"
}
}).then(({ results }) => this.setState({ customer: results }));
}
render() {
console.log(this.state.customer)
const customers = this.state.customer.map((item, i) => (
<div>
<h1>{item.customer_id}</h1>
<h1>{item.prediction}</h1>
</div>
));
return (
<div id="layout-content" className="layout-content-wrapper">
<div className="panel-list">{customers}</div>
</div>
);
}
}
This is what the API looks like:
When I run this, the console.log() returns undefined which then means I get the error TypeError: Cannot read property 'map' of undefined
. I am not too sure how to fix this so any help would be appreciated! Thanks.
EDIT:
This is what the API returns when you go to the link:
{"prediction":["Roam in Rome",""]}