捕获类型错误:无法将未定义或空值转换为对象

问题描述 投票:0回答:1

我正在尝试使用 axios 获取一些数据,然后将其显示在表格中,但出现了错误

我的代码: enter image description here enter image description here

这就是我要求的地方: enter image description here

这是错误: enter image description here

我试过,axios的连接没有问题

javascript reactjs axios xmlhttprequest fetch-api
1个回答
0
投票

发生这种情况是因为您提供给

useState
的初始状态没有 rates 属性。您试图在第 8 行获取它,但初始渲染的值是“未定义”,因此在第 10 行您在未定义上调用
Object.keys
。 您可以在 useState 上使用 rate 属性提供默认值:

useState({ rates: {} })

或者在调用之前验证它是否存在

Object.keys

© www.soinside.com 2019 - 2024. All rights reserved.