#Element implicitly has an 'any' type because index expression is not of type 'number'.

15 messages · Page 1 of 1 (latest)

silent idol
#

I'm trying to call an url gathered from the api request but im getting the error "Element implicitly has an 'any' type because index expression is not of type 'number'."

interface Repo {
  id: number;
  name: string;
  description: string;
  html_url: string;
  fork: string;
  languages_url: string;
}
interface Data {
  message: string;
}
interface LanguageData {
  [key: string]: number;
}

export default function Projects() {
  async function repoFetch() {
    let r = await axios.get("../repos");
    return Object.values<Repo>(r.data);
  }

  const {
    data: repo,
    isError,
    error,
  } = useQuery("Repos", repoFetch, {
    refetchOnWindowFocus: false,
  });

  async function languageFetch() {
    if (!repo) {
      return null;
    }
    let res = await axios.get<LanguageData>(repo["languages_url"]); ///error here
    return res.data
  }

  const { data: language } = useQuery("Languages", languageFetch, {
    refetchOnWindowFocus: false,
    enabled: !!repo,
  });
silent idol
#

?

reef mica
#

why not just do repo.languages_url?

silent idol
reef mica
#

if repo is an array of Repo, you need to access one of the indices first, like repo[0].languages_url

silent idol
#

yea but that just gives me the first element I need all of them

reef mica
#

What are you trying to do specifically?

#

you likely have the wrong approach

silent idol
#

yea probably

reef mica
#

If you need to perform an action on every "repo", you can loop through them

#

repo.forEach(repo => {/*...*/})

silent idol
#

Im calling an api which returns multiple properties but one property which i want to access to returns an api url and i need to call that to access those values

silent idol
#

like right now this is my approach

    const promises = repo.map(async (r) => {
      const res = await axios.get(r.languages_url);
      return res.data as LanguageData;
    });
    const results = await Promise.all(promises);
    return results as LanguageData[];
reef mica
#

in your repoFetch function, you're returning Object.values, is there a reason for that?