Reactjs 查找从API调用返回未定义的函数
我有一个函数,它传入一个ID,该ID获取一个不支持按特定ID搜索的API,因此我尝试构建一个查找函数,搜索ID并返回它 我以前做过这项工作,但我将函数移动到它们自己的文件中,似乎传入值可能会破坏函数?我没有从函数中得到任何错误,但可能我编写的find函数不正确Reactjs 查找从API调用返回未定义的函数,reactjs,api,fetch,react-hooks,Reactjs,Api,Fetch,React Hooks,我有一个函数,它传入一个ID,该ID获取一个不支持按特定ID搜索的API,因此我尝试构建一个查找函数,搜索ID并返回它 我以前做过这项工作,但我将函数移动到它们自己的文件中,似乎传入值可能会破坏函数?我没有从函数中得到任何错误,但可能我编写的find函数不正确 import React from 'react' export const fetchHeroDetail = async (id) => { const data = await fetch(`https://api.
import React from 'react'
export const fetchHeroDetail = async (id) => {
const data = await fetch(`https://api.opendota.com/api/heroStats`)
const heroDetails = await data.json()
console.log(id)
console.log(heroDetails)
console.log(heroDetails.find(heroDetail => id === heroDetail.id))
return heroDetails.find(heroDetail => id === heroDetail.id)
};
所以基本上我需要做的就是将id从字符串转换成数字。我只是通过做这些来完成的
return heroDetails.find(heroDetail => heroDetail.id === +id)
我看不出有什么内在的错误,但是,通过查看
console.log(id)
和console.log(heroDetails)
的结果,应该可以很容易地找出问题所在。这两种方法都给出了人们所认为的结果。id=我点击的任何英雄,heroDetails返回英雄的完整对象