Reactjs-App.js:34 Uncaught TypeError:c.map不是函数



当我从Rest API进行简单的GET请求时,我得到的是ch.map并不是我的application.js文件中的函数错误:

function App() {
const [ch, setCh] = useState([]);
useEffect(()=>{
fetch("https://localhost:12354/api/v1/Objects", {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic YWRt'
}
})
.then( resp => resp.json())
.then( resp => setCh(resp))
.catch( error => console.log(error))

}, [])
return (
<div className="App">
<header className="App-header">
<h1>Object Manager</h1>
</header>
<div className = 'layout'>
<div> 
{ ch.map( i => {
return <h2>{i.Name}</h2>
})} 
</div>
<div> Object Details </div>
</div> 
</div>
);
}

这是来自poster的相同API调用的输出。我正试图获得";名称";在我上面的javascript中:

{
"@odata.context": "$metadata#Object",
"value": [
{
"@odata.etag": "W/"24979988a17d428c"",
"Name": "Load Actual",
"StartTime": "2021-12-23T22:46:24+10:00",
"DSTSensitive": true,
"Active": true,
"ExecutionMode": "MultipleCommit",
"Frequency": "P1DT00H00M00S",
"Attributes": {
"Caption": "Load Actual"
}
}
]
}

setCh(resp)更改为setCh(resp.value)。当您调用setCh(resp)时,您将ch设置为等于整个响应json。这意味着它是具有关键字@odata.contextvalue的映射。方法映射不存在于对象上,但存在于resp.value上,resp.value是一个数组。

最新更新