【发布时间】:2020-08-10 18:16:18
【问题描述】:
我试图了解用于获取 fetch 的 .then 捕获。目前我有以下我知道有效的 POST 请求。当我检查data 变量时,有一个数据数组。
fetch(`http://PRIVATE/stocks/authed/${query}?from=${convertedStart}T00%3A00%3A00.000Z&to=${convertedEnd}T00%3A00%3A00.000Z`, requestOptions)
.then(response => response.json())
.then(data => setStockData(data));
我想应用一些错误处理,我试图在下面的以下更新代码中进行。但是,一旦我这样做了,data 变量就不再像上面的原始代码那样填充数据数组。为什么会这样,我该如何解决?
fetch(`http://PRIVATE/stocks/authed/${query}?from=${convertedStart}T00%3A00%3A00.000Z&to=${convertedEnd}T00%3A00%3A00.000Z`, requestOptions)
.then(response => {
if(!response.ok){
if(response.status === 404){
setError("Stock symbol not found please try again");
}
else{
setError("Please check your connection to the database");
}
}
else {
return response.json();
}
})
.then(data => setStockData(data));
【问题讨论】:
-
您确定响应中有 200 代码吗?也许响应包含正确的数据,但状态码不是
ok -
setError是做什么的?
标签: javascript fetch response