【发布时间】:2021-10-28 18:28:20
【问题描述】:
function News() {
const[stories, setStories] = useState([]);
useEffect(() => {
const getStoriesData = async () => {
await fetch("https://hn.algolia.com/api/v1/search_by_date?tags=(story,poll)")
.then((response) => response.json())
.then((data) => {
const stories = data.hits.map(({title,url,points,children}) => ({title,url,points,children}))
console.log(stories)
setStories(stories);
});
}
getStoriesData();
}, []);
return(
<div className="news">
<table className = "table">
<tr ><th>Title</th> <th>Points</th></tr>
{stories.map((story) => (
<tr>
<td><a className="title" href={story.url}>{story.title}</a></td>
<td><div className="points">{story.points}</div></td>
<td><div className="comments">{story.children.length}</div></td>
</tr>
))}
</table>
</div>
)
}
我必须在给定 API 中显示给定 ID 的所有 cmets(“children”字段)列表,但我不知道如何访问它。评论是树形的。 我尝试了上述方法,但没有奏效。 身份证前:https://hn.algolia.com/api/v1/items/12701272
【问题讨论】:
-
此端点
https://hn.algolia.com/api/v1/search_by_date?tags=(story,poll)在响应中没有名为 children 的键。
标签: javascript reactjs json api