【发布时间】:2021-08-22 14:36:44
【问题描述】:
所以,我正在构建一个字典库,试图使用 Axios 获取 API 数据,一切正常,但有时它会抛出 "TypeError: Cannot read property '0' of undefined " 对于某些单词。可能是什么问题?因为当我尝试获取某些单词的含义时,它可以正常工作,但对于某些单词,它会引发错误。代码如下
function App() {
const [mapvalue, setMapValue] = useState([])
const reqLibrary = () => {
axios.get(dictionaryapi)
.then((respone) => {
setMapValue(respone.data.def[0].tr)
//console.log(respone.data.def[0].tr[0].mean)
})
}
return (
</div>
<button onClick={reqLibrary}> Get Words </button>
<ul>
{mapvalue.map((response) => (
<li> {response.mean[0].text} </li>
))}
</ul>
</div>
)
}
【问题讨论】: