【发布时间】:2023-04-01 02:07:02
【问题描述】:
我正在尝试遍历通过调用 API 获得的对象但是在将数据设置为状态 setNodesData 并从 nodesData 获取数据之后,但是当我尝试这样做时,我收到一条错误消息: TypeError: Object.entries requires that input parameter not be null or undefined in React Native
我也尝试过for(const i in nodesData),但我仍然收到错误消息expression expected
useEffect(() => {
if (apiHeaders) {
axios
.get(
"https://test-myways-anfhsya-asq.myways.in/api/****",
{
headers: apiHeaders,
}
)
.then((res) => {
console.log("API Call")
console.log(res?.data);
setNodesData(res?.data);
})
.catch((err)=>{console.log("error:",err)});
}
}, [apiHeaders]);
return (
{
Object?.entries(nodesData).forEach((i) => (
<View style={{ padding: 5 }}>
<View
style={{
flexDirection: "row",
alignItems: "center",
}}
>
<TouchableOpacity
onPress={(props) => props.navigation.navigate("CoursesPlaylistScreen")}
style={{
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
flex: 1,
}}
>
<View
style={{
flexDirection: "row",
justifyContent: "space-between",
flex: 1,
}}
>
<Text
style={{
fontSize: 16,
color: "#000000",
fontWeight: "bold",
}}
>
{i}
</Text>
</View>
</TouchableOpacity>
</View>
数据:
{
"week1": {
"content": [
{
"moduleName": "HTML Crash course",
"moduleVideos": [
{
"title": "HTML Crash Course For Absolute Beginners",
"link": "https://youtu.be/*****",
"duration": "01:00:42"
}
]
},
{
"moduleName": "CSS Crash course",
"moduleVideos": [
{
"title": "CSS Crash Course For Absolute Beginners",
"link": "https://youtu.be/*****",
"duration": "01:25:11"
}
]
},
我只想获得几周,但我遇到了错误,我不知道该怎么做。
【问题讨论】:
-
请编辑您的帖子以仅包含查找问题所需的minimal, reproducible example。附加整个文件 - 超过一百行可能不相关的代码 - 只会让问题更难找到。
-
@EldarB。哦。好的,谢谢我已经编辑了帖子。
标签: javascript reactjs react-native api object