I want my React render method to return objects from an API in a similar way to what I get from Postman. For example:
{
"meta": {
"count": 807,
"countReturned": 10,
"requestTime": 1552524395,
"responseTime": 1552524395,
"responseMs": 7
},
"data": [
{
"type": "breeds",
"id": "1",
"attributes": {
"name": "Abyssinian"
},
"relationships": {
"species": {
"data": [
{
"type": "species",
"id": "3"
}
]
}
},
"meta": []
},
But I'm having trouble using .map to produce the object(s) I want. Here is my code:
class Results extends Component {
constructor() {
super();
this.state = {
animals: [],
};
}
componentDidMount() {
var url = "https://test1-api.rescuegroups.org/v5/public/animals/breeds?fields[breeds]=name&fields[species]=singular,plural,youngSingular,youngPlural&include=species&options=meta&limit=10";
const API_KEY = process.env.REACT_APP_API_KEY;
fetch(url, {
method: 'GET',
headers: {
Authorization: API_KEY,
'Content-Type': 'application/json'}
})
.then(response => response.json())
.then(data => this.setState({animals: data.results }))
}
render() {
return (
<div>
{this.state.animals.map(animal => <div>{animal.results}</div>)}
</div>
)
}
}
export default Results;
Any tips are appreciated!
then(data => this.setState({animals: data.results })), according to your json, there is noresultsfield in it.