【发布时间】:2022-01-14 16:29:34
【问题描述】:
我正在使用 React Native Fetch 检索 JSON 对象,我的目标是将其呈现为屏幕上的下拉菜单。但是,在处理渲染部分之前,我在 componentDidMount() 上收到此错误“TypeError: Cannot read properties of undefined (reading 'Obj') React Native”,我不确定问题是什么。
fetch("https://jsonplaceholder.typicode.com/todos/?_limit=10") // **Api for fetching**
.then(response => response.json())
.then((responseJson) => {
var count = Object.keys(responseJson.message.Obj).length;
let drop_down = [];
for(var i=0;i<count;i++){
console.log(responseJson.message.Obj[i].title) // I need to add
drop_down.push({ value: responseJson.message.Obj[i].title }); // Creating array of data
}
this.setState({ drop_down }); // Set the new state
})
.catch(error =>console.log(error)) //to catch the errors if any
}
提前致谢。
【问题讨论】:
-
检查您在
responseJson.message中获得的内容,它似乎不存在,因此出现错误