I’m creating a front-end to a back-end I’ve created. I’ve got my system working where if the back end is online, the front-end displays the requested data from my back-end API. I currently do this using the JavaScript fetch function like this:
fetch("http://localhost:3001/public-key")
.then((response) => response.json())
.then(returnedKey => {
this.setState({ publicKey: returnedKey })
})
.catch(err => {
console.log("error")
this.setState({ publicKey: "Error" })
});
What I want to do is catch the CORS request did not succeed message when my back-end is down and set the state of publicKey to an error so it displays “error” to the user, however the way I’m trying to do it with catch doesn’t seem to be working.
Any insight would be greatly appreciated.