Answer the question
In order to leave comments, you need to log in
How can I hide the error in the console?
When sending data to the backend, the data is checked, and if something is wrong, react informs the user about it.
But for some reason an error appears in the console... Is it supposed to be like this? Do I need to hide the error and not show it in the console? How to hide the error?
export const registerUser = (data, history) => dispatch => {
fetch("/api/auth/register", {
method: "post",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})
.then(res => {
return res.json();
})
.then(res => {
if (res.access_token) {
const { access_token } = res;
localStorage.setItem('jwtToken', access_token);
const decoded_jwt = jwt_decode(access_token);
dispatch(setCurrentUser(decoded_jwt));
history.push('/');
}
else if (res.errors) {
dispatch({
type: 'GET_ERRORS',
payload: res.errors,
})
}
})
.catch(err => {
dispatch({
type: 'GET_ERRORS',
payload: err,
})
});
}
Answer the question
In order to leave comments, you need to log in
401 is a normal server response to incorrect data during authorization.
There is no need to hide it, since the console is intended for the developer (not for nothing that the section in chrome is called Chrome DEV tools), and not for the user. For such errors, it will be much easier to debug yourself.
It is important for the user to display an understandable error in the interface.
Just change the response code to any of the range 200-299. Code 400+ means that the client did something wrong , so he will see an error. Sometimes it won’t see it without a console, sometimes it will see the number of errors in some plugin and it will become worse for your service.
400+ errors should be thrown when someone makes a bad request . That is, when the client (human or js application) can know that this is not allowed. And if this is an authorization check, then this is a normal request in which there is no error.
When the request is correct, the client is not mistaken , you need to give the code 200-299.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question