I'm trying to use WordPress API with react, but it returns id instead of the tag name, so I'm trying to fetch the tag name with another API call. but it keeps returning undefined. When I add return before fetch inside getCategory() it just errors out.
componentDidMount() {
const URL =
'https://public-api.wordpress.com/wp/v2/sites/sitename/posts/';
fetch(URL)
.then(res => res.json())
.then(posts => {
const post = posts.map(post => {
return {
...post,
categories: this.getCategory(...post.categories)
};
});
this.setState({ posts: post });
console.log(post);
})
.catch(err => console.log(err));
}
getCategory(id) {
const URL = `https://public-api.wordpress.com/wp/v2/sites/sitename/categories/${id}`;
fetch(URL)
.then(data => data.json())
.then(res => res.name)
}
getCategoryexpects a single id, but you are spreading all thepost.categoriesthere.getCategoryhas noreturnvalue, and even if you return thefetch, you are returning aPromise. Also, you might want to edit out your URLs