【发布时间】:2022-01-01 15:10:20
【问题描述】:
我正在尝试使用 fetch API 获取响应,而不是陷入错误,如下所述。这是我的代码
const DefaultLayout = () => {
let history = useHistory()
const callHomePage = async () => {
try {
const res = fetch('http://localhost:4000/api/authenticate', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
},
credentials: 'include',
})
console.log(res)
const data = await res.json()
console.log(data)
if (!res.status === 200) {
const error = new Error(res.error)
throw error
}
} catch (err) {
console.log(err)
history.push('login')
}
}
错误: TypeError:res.json 不是函数 承诺 {} 显示待处理
【问题讨论】:
-
您需要
awaitinconst res = await fetch(...)
标签: javascript reactjs json express