REACT:Unturef TypeError:Animes.map不是功能

发布于 2025-02-12 23:53:28 字数 1312 浏览 0 评论 0 原文

我是新手反应,并且正在进行动漫项目。我设法通过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;```

I'm new to React and I'm doing an Anime project. I've managed to get the values of the animes from the Jikan API with the map function. But the url's correspond to for example "https://api.jikan.moe/v4/genres/anime" or "https://api.jikan.moe/v4/top/anime?filter=bypopularity", but if i change that same url to a specific character (for example: "https://api.jikan.moe/v4/people/5579") or a specific movie (for example "https://api.jikan.moe/v4 /anime/38000") tells me that "animes.map is not a function". I've verified that the parameters are correct. I would appreciate your help.


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

万水千山粽是情ミ 2025-02-19 23:53:28

如果要直接访问人ID,则需要删除地图

“


import {useState, useEffect} from 'react';


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">
            <div key={animes.mal_id}>
              <h2>{animes.name}</h2>
              { console.log(animes) }
            </div>
        </article>
      </main>
    </>
    // </React.Fragment>
  );
}

export default App

If you want to access a people Id directly then you need to remove map

Codesandbox Demo


import {useState, useEffect} from 'react';


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">
            <div key={animes.mal_id}>
              <h2>{animes.name}</h2>
              { console.log(animes) }
            </div>
        </article>
      </main>
    </>
    // </React.Fragment>
  );
}

export default App
困倦 2025-02-19 23:53:28

如果您需要同时支持 https://api.jikan.moe/v4/genres/genres/genres/genres/anime/anime 搜索和
特定角色,例如 https://api.jikan.moe/v4/people/5579 然后,在响应时间您可以检查响应是否是数组,如果不是数组,则可以将其转换为数组,以便您

{animes.map((anime) => (
        <div key={anime.mal_id}>
          <h2>{anime.name}</h2>
        </div>
      ))}

没有休息,在这里您可以做到这一点,

    const getAnimes = async () => {
  try {
    const response = await fetch('https://api.jikan.moe/v4/people/5579');
    let animes = await response.json();
    if (!Array.isArray(animes)) {
      animes = [animes]
    }
    return animes;
  } catch (error) {
    console.error(error);
  }
};

但是尽管这不是一个好习惯。您应该拥有一个列出所有动漫的页面,当用户单击单个动漫时,您应该有一个单独的页面,这将有助于采用更确定性的方法。

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

{animes.map((anime) => (
        <div key={anime.mal_id}>
          <h2>{anime.name}</h2>
        </div>
      ))}

does not get breaks, here how you can do that

    const getAnimes = async () => {
  try {
    const response = await fetch('https://api.jikan.moe/v4/people/5579');
    let animes = await response.json();
    if (!Array.isArray(animes)) {
      animes = [animes]
    }
    return animes;
  } catch (error) {
    console.error(error);
  }
};

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.

黄昏下泛黄的笔记 2025-02-19 23:53:28

您正在尝试 MAP 对象不是 array 是不可能的。如果要获取所有 People 信息,则应从 api 呼叫中删除 ID ,则可以正常工作。

const response = await fetch("https://api.jikan.moe/v4/people");

这是没有 ID https://codesandbox.io/embed/thirsty-feistel-yjs6my?fontsize = 14&amp;hidenavigation=1&amp;theme=dark

相反,如果要使用单人对象,只需删除 MAP 零件

You are trying to map an object not an array which is not possible. If you want to get all people information, you should remove the id from api call then it will work fine.

const response = await fetch("https://api.jikan.moe/v4/people");

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文