0

I am using rick & morty API, and i want to render cards of the result where I display name, gender, location & episode name. But the object returns this.

>  "results": [
        {
            "id": 19,
            "name": "Antenna Rick",
            "status": "unknown",
            "species": "Human",
            "type": "Human with antennae",
            "gender": "Male",
            "origin": {
                "name": "unknown",
                "url": ""
            },
            "location": {
                "name": "unknown",
                "url": ""
            },
            "image": "https://rickandmortyapi.com/api/character/avatar/19.jpeg",
            "episode": [
                "https://rickandmortyapi.com/api/episode/10"
            ],
            "url": "https://rickandmortyapi.com/api/character/19",
            "created": "2017-11-04T22:28:13.756Z"
        }
    ]

Given the episode array, I need to show the name of the episode, not the URL. So what I do in my return is this

<h5>{'Episode: '+getEpisode(item.episode)}</h5>

 const getEpisode = (item) => {
**//REQUEST TO https://rickandmortyapi.com/api/episode/10**
    Requests.getEpisode(item[0]).then((res)=>{
        return res.name
    })
    .catch((err)=>{
        console.log(err)
    })
}

But all I got it's undefined printed in the h5 tag, why is this ? can anybody help me

this is the content of request.getEpisode

  const getEpisode = async (URL) => { 
//URL = https://rickandmortyapi.com/api/character/19
const {data: res} = await axios.get(`${URL}`);
return res;

};

Jose A.
  • 483
  • 2
  • 7
  • 15
  • 1
    what is Requests? ive never seen that. your question is tagged with axios so i will try to answer with that – Keaton Benning Jan 20 '22 at 03:24
  • Can you post the content of `Requests.getEpisode`? I made the assumption in my answer that this is your own method that is probably calling `axios` under the hood, but it would make your question clearer if you provide the code for that as well. – elethan Jan 20 '22 at 03:30

1 Answers1

2

I can't tell what your Requests.getEpisode function does, but if you are using axios like your tags suggest, you could do something like this:

const getEpisode = (item) => {
    axios.get(item[0]).then((res)=>{
        return res.data.name
    })
    .catch((err)=>{
        console.log(err)
    })
}

Depending on what your Requests.getEpisode() function looks like, you might just need to update the getEpisode function that you have in your post to return res.data.name instead of res.name

Update:

Based on the new code OP has shared, I don't think there are any issues with how the request is made. I think the issue is that getEpisode(item.episode) doesn't return anything. To solve this, you could try updating a state variable after your episode is retrieved from the api. Something like this:

import React from "react";
import axios from "axios";

const getEpisode = (item) => {
  return _getEpisode(item[0])
    .then((res) => {
      return res.name;
    })
    .catch((err) => {
      console.log(err);
    });
};

const _getEpisode = async (URL) => {
  const { data: res } = await axios.get(`${URL}`);
  return res;
};

export default function App() {
  const [episode, setEpisode] = React.useState("");

  React.useEffect(() => {
    getEpisode(["https://rickandmortyapi.com/api/episode/10"]).then((res) => {
      setEpisode(res);
    });
  });

  return <h5>{"Episode: " + episode}</h5>;
}

The api data is retrieved in the useEffect hook, and then state is updated so that the episode title displays in your component. You can see it working in this sandbox

elethan
  • 16,408
  • 8
  • 64
  • 87
  • @Phil it looks like the consuming code is `
    {'Episode: '+getEpisode(item.episode)}
    `, so I think the function would have to return the string name, right (rather than the object)? Or maybe I am missed something?
    – elethan Jan 20 '22 at 03:28
  • Sorry, I thought you were rewriting `Request.getEpisode` as opposed to just `getEpisode` – Phil Jan 20 '22 at 03:29
  • 1
    Got it. Yes, that part is confusing. I am not sure exactly what `Request.getEpisode` is... – elethan Jan 20 '22 at 03:29
  • Request.getEpisode basically it's a get from https://rickandmortyapi.com/api/episode/10, so this returns me an object and I should print the name from the object – Jose A. Jan 20 '22 at 03:31
  • OK, I updated my answer. I misunderstood the original problem, but I have updated with a working solution. – elethan Jan 20 '22 at 04:06