我有一个问题,我可以获取console.log数据,但是当我刷新页面时,我得到了错误

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

我有一个问题,我可以获取console.log数据,但是当我刷新页面时,我得到了错误。

console.log(city.value.terms[0].value)
,

它向我显示数据

Melbourne

enter image description here

但是当我刷新页面时,它显示错误。

Unexpected Application Error! Cannot read properties of undefined (reading 'terms')

enter image description here

哪里有问题??

reactjs react-native
1个回答
0
投票

根据您提供的屏幕截图,

city
在此处定义为空字符串
const [city, setCity] = useState("")

因此,您尝试执行的操作相当于

"".value.terms[0].value
,这意味着您正在尝试访问字符串上的属性。
"".value === undefined
因为字符串上没有这样的属性。您需要将
city
的值设置为具有您期望的结构的对象。

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