I'm trying to understand the .then
catch that is applied to fetch fetch
. Currently I have the following POST request that I know works. When I examine the data
variable there is an array of 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));
I want to apply some error handling which ive attempted to do in the following update piece of code below. However once i do this the data
variable no longer populates with an array of data like it did in the original piece of code above. Why is this and how do i fix it?
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));