代码之家  ›  专栏  ›  技术社区  ›  Carlos Henrique R. Pereira

我的API请求有效,但无法访问内容

  •  -1
  • Carlos Henrique R. Pereira  · 技术社区  · 2 年前

    我提出了API请求:

    const [animes, setAnimes] = useState([]);
    
    useEffect(() => {
        axios
            .get("https://api.jikan.moe/v4/anime/38000/characters")
            .then((response) => {
                setAnimes(response.data);
            })
            .catch((err) => {
                console.log(err);
            });
    }, []);
    

    当我这么做的时候 console.log(animes) 我收到的数据包含76个项目。但是,当我这样做时:

    <div className="container">
                    {!!animes.length &&
                        animes?.slice(0, 9).map((anime, index) => {
                            return (
                                <img
                                    key={index}
                                    src={anime.image_url}
                                    alt={anime.name}
                                />
                            );
                        })}
                </div>
    

    我拿不到img和alt。控制台。日志结果是一个包含数组的对象。但是如果我使用“setAnimes(response.data.data)”,我只会收到数组,即使这样,我也无法获得内容。

    如果我控制台。日志(setAnimes)我收到:

    function dispatchSetState(fiber, queue, action) {
    {
    if (typeof arguments[3] === 'function') {
      error("State updates from the useState() and useReducer() Hooks don't support the " + 
      'second callback argument. To execute a side effect after ' + 'rendering, declare it 
      in the component body with useEffect().');
      }
     }
    
     var lane = requestUpdateLane(fiber);
     var update = {
     lane: lane,
     action: action,
     hasEagerState: false,
     eagerState: null,
     next: null
     };
    

    2 回复  |  直到 2 年前
        1
  •  0
  •   Mina    2 年前

    这是因为动画对象不包含 image_url name

    它只包含这些属性

    1. 角色:对象
    2. 角色:字符串
    3. voice_演员:阵列

    检查控制台。

    https://codesandbox.io/s/tender-hopper-ytxkvc

        2
  •  -1
  •   Abdullah    2 年前

    你在找 image\u url 名称

    使用 anime.character.images.jpg.image_url anime.character.name
    属性