Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 查找从API调用返回未定义的函数_Reactjs_Api_Fetch_React Hooks - Fatal编程技术网

Reactjs 查找从API调用返回未定义的函数

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.

我有一个函数,它传入一个ID,该ID获取一个不支持按特定ID搜索的API,因此我尝试构建一个查找函数,搜索ID并返回它

我以前做过这项工作,但我将函数移动到它们自己的文件中,似乎传入值可能会破坏函数?我没有从函数中得到任何错误,但可能我编写的find函数不正确

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返回英雄的完整对象