Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My API request works but I can't access the content

I made a API request:

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);
        });
}, []);

when I do console.log(animes) I receive the data with a array of 76 items. But, when I do:

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

I can’t get the img and the alt. The console.log result is a object with a array inside. But if I use "setAnimes(response.data.data)" I receive just the array and even with this I can’t get the content.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If I console.log(setAnimes) I receive this:

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
 };

What I’m doing wrong?

>Solution :

You’re looking for image_url and name in the wrong object.

use anime.character.images.jpg.image_url and anime.character.name to refer to correct
properties

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading