REACT:Unturef TypeError:Animes.map不是功能
我是新手反应,并且正在进行动漫项目。我设法通过MAP功能从Jikan API中获取动漫的值。但是URL与例如“ https://api.jikan.moe/v4/genres/anime”或“ https://api.jikan.moe/v4/top/top/anime?filter = bypopularity”将相同的URL更改为特定字符(例如: “ https://api.jikan.moe/v4/people/5579”)或特定的电影(例如“ https://api.jikan.moe/v4/nime/38000”)告诉我,不是功能”。我已经验证了参数是正确的。感谢您的帮助。
const getAnimes = async () => {
try {
const response = await fetch('https://api.jikan.moe/v4/people/5579');
const animes = await response.json();
return animes;
} catch (error) {
console.error(error);
}
};
function App() {
const [animes, setAnimes] = useState([]);
const fetchAnimes = async () => {
try {
const data = await getAnimes();
setAnimes(data.data);
} catch (error) {}
};
useEffect(() => {
fetchAnimes();
}, []);
return (
// <React.Fragment>
<>
<header>
<h1>Search Anime</h1>
</header>
<main>
<article className="countries">
{animes.map((anime) => (
<div key={anime.mal_id}>
<h2>{anime.name}</h2>
</div>
))}
</article>
</main>
</>
// </React.Fragment>
);
}
export default App;```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要直接访问人ID,则需要删除
地图
If you want to access a people Id directly then you need to remove
map
如果您需要同时支持 https://api.jikan.moe/v4/genres/genres/genres/genres/anime/anime 搜索和
特定角色,例如 https://api.jikan.moe/v4/people/5579 然后,在响应时间您可以检查响应是否是数组,如果不是数组,则可以将其转换为数组,以便您
没有休息,在这里您可以做到这一点,
但是尽管这不是一个好习惯。您应该拥有一个列出所有动漫的页面,当用户单击单个动漫时,您应该有一个单独的页面,这将有助于采用更确定性的方法。
If you need to support both The https://api.jikan.moe/v4/genres/anime search and
specific character like https://api.jikan.moe/v4/people/5579 Then on the response time you can check whether response you get is an array or not, If its not you can convert it to an array so your
does not get breaks, here how you can do that
but though it is not a good practice to do in that way. You should have a page which lists all animes and when a user click on a single anime you should have a separate page for it, it will help to have a more deterministic approach.
您正在尝试
MAP
对象
不是array
是不可能的。如果要获取所有People
信息,则应从api
呼叫中删除ID
,则可以正常工作。这是没有
ID
https://codesandbox.io/embed/thirsty-feistel-yjs6my?fontsize = 14&amp;hidenavigation=1&amp;theme=dark 。相反,如果要使用单人对象,只需删除
MAP
零件You are trying to
map
anobject
not anarray
which is not possible. If you want to get allpeople
information, you should remove theid
fromapi
call then it will work fine.Here is the working version without
id
https://codesandbox.io/embed/thirsty-feistel-yjs6my?fontsize=14&hidenavigation=1&theme=dark.Instead if you want to use single people object, just remove the
map
part